Stormix
Stormix

Reputation: 113

Problems with PHP , Mysqli and Dropzone

Hi I am just wondering if someone can tell me what I'm doing wrong.I am trying to upload some files using dropzone and insert a record of this file on my database .
This is my app demo : http:/stormix.co/projects/megadrop/index
I can easily upload files and add a record of these files in my database using a normal file input (u can test that in the link I gave u).
But when I try to do that with dropzone it just fails
This is what I use to handle my dropzone request :

//Upload file
if (!empty($_FILES)) {    
 /* Define variables */
 $file = $_FILES['file'];
 //Get Directory Separator : usually it's "/"
 $ds  = DIRECTORY_SEPARATOR;
 //Select the upload directory : this is where files will go :)
 $storeFolder = '..'. $ds.'drop'; 
//Convert the file size to byte
 $max_size = 10 *1024 *1024;
 //Get File name
 $filename = $file['name'];
 $tempfile = $file['tmp_name'];     
//Define the upload/target directory 
 $targetpath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
 //Define new target
 $targetfile =  $targetpath.time()."_".$file['name'];
//Check if $_FILES variable is now empty
if (!empty($_FILES)) {
//Check if file type is allowed
  if($allowed_files == "All" OR in_array($file['type'],$allowed_files) OR  in_array("All",$allowed_files)){
  //Check if any error occurred
   if($file['error'] == "0"){
   //Check if file is smaller than file size
    if($file['size'] <=  $max_size){
    //Check if file is uploaded
    if(move_uploaded_file($tempfile,$targetfile)){
    /*------------------------------------------------------------------------------------------------------------ */
    //Mysql commands
     $filename =  $mysqli->real_escape_string($file['name']) ;
     $count = substr_count($filename, '.');
     $ext =  explode('.',$filename);
     $type = $mysqli->real_escape_string(strtoupper($ext[$count])." File");
     $uploader =$mysqli->real_escape_string($_SESSION['user']);
     $key = substr(md5(rand(0,99999999)),0,8);
     $timestamp = time();
     $date = date('Y-m-d H:i:s');
     $size = bytesToSize($file['size']);
     $targetfile = $mysqli->real_escape_string($targetfile);
     if ($mysqli->query("INSERT INTO files VALUES ('','".$filename."','". $type ."','Uploaded','".$uploader."','".$key."','". $size."','0','0','".$timestamp."','".$date."','".$targetfile."')")) {
    return true;
     }else{
     echo  'An error occurred while inserting file into database !<br>Error : <b>[' . $mysqli->error . ']</b>'; 
     return false;
     unlink($targetfile);
     }
     /* ------------------------------------------------------------------------------------------------------------ */
     }else{echo  "An error occurred while uploading."; return false;}
    }else{echo   "File is too big !</b>"; return false;}
   }else{echo  "An error occurred while uploading. <br>Error Code :".$file['error'] .""; return false;}
  }else{echo  "Invalid File Type"; return false;}
 }else{echo  "No files were selected"; return false;}

}else{header("location: index");}

I'm saving the files on a directory called : drop
The max file size is 10MB
This is the first time I ask something here , so sorry if something is missing ; just tell me and I will add it
And Also ,I would like to know how to return errors from php to dropzone , cuz I don't know how :(

Upvotes: 0

Views: 160

Answers (1)

Stormix
Stormix

Reputation: 113

My code contained a lot of errors :
- $allowed_files was undefined : I have it set to ALL as in my config file
- $targetpath was uncorrect , so I had it fixed
I used :
header('Content-type: text/plain');
exit("My error");
to display the errors , now all are fixed :D

The new code is : http://pastebin.com/CSjn7UPC

Upvotes: 1

Related Questions