PHP,Mysqli和Dropzone的问题

时间:2015-08-03 19:26:49

标签: php mysqli dropzone.js

您好我想知道是否有人可以告诉我我做错了什么。我正在尝试使用dropzone上传一些文件并在我的数据库中插入此文件的记录。
这是我的应用程序演示:http:/stormix.co/projects/megadrop/index
我可以使用普通文件输入轻松上传文件并在我的数据库中添加这些文件的记录(你可以在链接中测试它我给了你。)但是当我尝试用dropzone做那件事时,它就失败了 这是我用来处理我的dropzone请求的内容:

//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");}

我将文件保存在名为:drop的目录中
最大文件大小为10MB 这是我第一次在这里问一些事情,如果遗漏了什么,那就很抱歉;告诉我,我会添加它
另外,我想知道如何将错误从php返回到dropzone,因为我不知道如何:(

1 个答案:

答案 0 :(得分:1)

我的代码包含很多错误:
- $ allowed_files未定义:我在配置文件中设置为ALL - $ targetpath是不正确的,所以我把它固定了 我用过:
header('Content-type: text/plain');
exit("My error");显示错误,现在所有错误都已修复:D

新代码为:http://pastebin.com/CSjn7UPC