file upload script error ,please help ?

PHP Code:

<?php
include_once ‘dbconfig2.php’;
?>
<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>File Uploading With PHP and MySql</title>
<link rel=”stylesheet” href=”style.css” type=”text/css” />
</head>
<body>
<div id=”header”>
<label>File Uploading With PHP and MySql</label>
</div>
<div id=”body”>
    <form action=”upload2.php” method=”post” enctype=”multipart/form-data”>
    <input type=”file” name=”file” />
    <button type=”submit” name=”btn-upload”>upload</button>
    </form>
    <br /><br />
    <?php
    if(isset($_GET[’success’]))
    {
        ?>
        <label>File Uploaded Successfully…  <a href=”view2.php”>click here to view file.</a></label>
        <?php
    }
    else if(isset($_GET[’fail’]))
    {
        ?>
        <label>Problem While File Uploading !</label>
        <?php
    }
    else
    {
        ?>
        <label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc…)</label>
        <?php
    }
    ?>
</div>
<div id=”footer”>
<label>By <a href=”http://cleartuts.blogspot.com”>cleartuts.blogspot.com</a></label>
</div>
</body>
</html>

PHP Code:

<?php
class Database
{
     
    private $host = “localhost”;
    private $db_name = “dbtuts”;
    private $username = “root”;
    private $password = “root”;
    public $conn;
     
    public function dbConnection()
    {
     
        $this->conn = null;    
        try
        {
            $this->conn = new PDO(“mysql:host=” . $this->host . “;dbname=” . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
        }
        catch(PDOException $exception)
        {
            echo “Connection error: ” . $exception->getMessage();
        }
         
        return $this->conn;
    }
}
?>

PHP Code:

<?php
include_once ‘dbconfig2.php’;
if(isset($_POST[’btn-upload’]))
{    
     
    $file = rand(1000,100000).”-“.$_FILES[’file’][’name’];
    $file_loc = $_FILES[’file’][’tmp_name’];
    $file_size = $_FILES[’file’][’size’];
    $file_type = $_FILES[’file’][’type’];
    $folder=”uploads/”;
    
    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB
    
    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case
    
    $final_file=str_replace(‘ ‘,’-‘,$new_file_name);
    
    if(move_uploaded_file($file_loc,$folder.$final_file))
    {
        $sql=”INSERT INTO tbl_uploads(file,type,size) VALUES(‘$final_file’,’$file_type’,’$new_size’)”;
        mysql_query($sql);
        ?>
        <script>
        alert(‘successfully uploaded’);
        window.location.href=’index2.php?success’;
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert(‘error while uploading file’);
        window.location.href=’index2.php?fail’;
        </script>
        <?php
    }
}
?>

PHP Code:

<?php
include_once ‘dbconfig2.php’;
?>
<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>File Uploading With PHP and MySql</title>
<link rel=”stylesheet” href=”style.css” type=”text/css” />
</head>
<body>
<div id=”header”>
<label>File Uploading With PHP and MySql</label>
</div>
<div id=”body”>
    <table width=”80%” border=”1″>
    <tr>
    <th colspan=”4″>your uploads…<label><a href=”index2.php”>upload new files…</a></label></th>
    </tr>
    <tr>
    <td>File Name</td>
    <td>File Type</td>
    <td>File Size(KB)</td>
    <td>View</td>
    </tr>
    <?php
    $sql=”SELECT * FROM tbl_uploads”;
    $result_set=mysql_query($sql);
    while($row=mysql_fetch_array($result_set))
    {
        ?>
        <tr>
        <td><?php echo $row[’file’] ?></td>
        <td><?php echo $row[’type’] ?></td>
        <td><?php echo $row[’size’] ?></td>
        <td><a href=”uploads/<?php echo $row[’file’] ?>” target=”_blank”>view file</a></td>
        </tr>
        <?php
    }
    ?>
    </table>
    
</div>
</body>
</html>

Code:
CREATE DATABASE `dbtuts` ;
CREATE TABLE `dbtuts`.`tbl_uploads` […]

Original post by digimon

Leave a Reply