1

I'm working with Jquery EasyUI and I have a trouble with this code. I have a form to input a file to DB and the form has a input type = file that I want to allow an attachment. However, when the form is submitted, the $_FILES object has nothing in to DB and to the path declared.

I hope that somebody can help me to solve this trouble code. Thank you

Here is my listening code:


<form id="form" method="post" enctype="multipart/form-data">
    <input type="file" name="file_att" id="file_att" size = "30px;" class="easyui-validatebox" required="true"/>
</form>

Here is my code to run the form when the form submitted:


<?php
    include "../inc/inc.koneksi.php";

    $file_att=$_FILES["file_att"]["name"];
    $target="file_att/$file_att";
    $temp=$_FILES["file_att"]["tmp_name"];


    $query=mysql_query("INSERT INTO t_director(id_direc,file_att) VALUES ('NULL','$file_att')");
    if(!empty($file_att)){
        move_uploaded_file("$temp","$target");
        echo "Success Attached";
    }
    else {echo "Failed";}               
?>
Genzotto
  • 1,954
  • 6
  • 26
  • 45
  • 4
    First of all you should drop the mysql commands in favour of [mysqli or PDO](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php), since mysql is deprecated. Second, uploading a file without setting a size limit sounds like a bad idea to me. – Hristo Valkanov Jun 18 '14 at 06:08
  • Show your jQuery code. – Darren Jun 18 '14 at 08:31

1 Answers1

0

My bet is that you fail to upload the file for some reason. Try he following code:

if(move_uploaded_file($temp, $target)) {
    echo "File uploaded";
} else{
    echo "Error!";
}

instead of:

move_uploaded_file("$temp","$target");
echo "Success Attached";

if this outputs and error, try var_dump($_FILES["file_att"]); to get the error code. More info on the error codes can be found here.

Also, your file is located at $_FILES["file_att"]["tmp_name"] before you move it, thus you will save only it's file name inside the database, not the file itself.

Third: Like I stated inside the comments, use mysqli/PDO and, for your sanity's sake, create a file limit with

<input type="hidden" name="MAX_FILE_SIZE" value="XXX">

where XXX is the value in bytes.

EDIT: Also, check your server's post_max_size and upload_max_file_size.

Hristo Valkanov
  • 1,689
  • 22
  • 33