PHP upload filename to mysql

690 views Asked by At

This code transfers the file to the specified folder and db table but when I launch/open/run the page on the browser,it automatically sends something to the db table and the filename field is empty.I haven't even clicked/uploaded anything yet. I don't know if i explained it properly.The problem is when i opened the page,i checked the db table rows and an empty row was made.the id increments btw, and the filename field is empty.(not uploading anything yet.) What's wrong with the code?

<?php
if(isset($_FILES['filename'])){
    $errors= array();
    $file_name = $_FILES['filename']['name'];
    $file_size =$_FILES['filename']['size'];
    $file_tmp =$_FILES['filename']['tmp_name'];
    $file_type=$_FILES['filename']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));

    $expensions= array("jpeg","jpg","png");         
    if(in_array($file_ext,$expensions)=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }
    if($file_size > 2097152){
    $errors[]='File size must be excately 2 MB';
    }               
    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"uploads/".$file_name);
        echo "Success";
    }else{
        print_r($errors);
    }
  }

 ?>

<?php


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "admin";

$filename = false;
if(isset($_FILES['filename'])){
$filename = $_FILES['filename']['name'];
}

// Create connection
mysql_connect($servername, $username, $password) or die ('MySQL Not found // Could Not Connect.');

mysql_select_db("admin") or die(mysql_error()) ;

mysql_query("INSERT INTO upload_test (fileName)
VALUES ('$filename')") ;

?>

my form:

<form name="form" method="POST" enctype="multipart/form-data" >
<input name="filename" type="file" id="filename" />
<input name="submit" type="submit" id="submit"/>
</form>
1

There are 1 answers

3
Lynn Adrianna On BEST ANSWER

That is because you are always executing the INSERT statement. You only want insert a record once you have uploaded.

if(isset($_FILES['filename'])){
    $errors = array();
    $file_name = $_FILES['filename']['name'];
    $file_size =$_FILES['filename']['size'];
    $file_tmp =$_FILES['filename']['tmp_name'];
    $file_type=$_FILES['filename']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));

    $expensions= array("jpeg","jpg","png");         
    if(in_array($file_ext,$expensions)=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }
    if($file_size > 2097152){
     $errors[]='File size must be excately 2 MB';
    }          

    // if there are no errors...     
    if (empty($errors)==true) {

        // upload the file...
        move_uploaded_file($file_tmp,"uploads/".$file_name);

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "admin";

        // and create a new record in the database
        mysql_connect($servername, $username, $password) or die ('MySQL Not found // Could Not Connect.');
        mysql_select_db("admin") or die(mysql_error()) ;
        mysql_query("INSERT INTO upload_test (fileName) VALUES ('$file_name')") ;

        echo "Success";
    }else{
        print_r($errors);
    }
}

On a side-note, a shorter way to get the extension of file is to use pathinfo()

$file_ext = pathinfo($_FILES['filename']['name'], PATHINFO_EXTENSION);