PHP 、Mysqli 和 Dropzone 的问题
Problems with PHP , Mysqli and Dropzone
你好我只是想知道是否有人可以告诉我我在做什么wrong.I我正在尝试使用 dropzone 上传一些文件并在我的数据库中插入该文件的记录。
这是我的应用程序演示:http:/stormix.co/projects/megadrop/index
我可以使用普通文件输入轻松上传文件并在我的数据库中添加这些文件的记录(你可以在 link 我给了你)。
但是当我尝试用 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
第一次在这里提问,如有遗漏请见谅;告诉我,我会添加它
而且,我想知道如何 return 从 php 到 dropzone 的错误,因为我不知道如何:(
我的代码包含很多错误:
- $allowed_files 未定义:我将其设置为 ALL,如我的配置文件
- $targetpath 不正确,所以我修复了它
我用过:
header('Content-type: text/plain');
exit("My error");
显示错误,现在都已修复:D
你好我只是想知道是否有人可以告诉我我在做什么wrong.I我正在尝试使用 dropzone 上传一些文件并在我的数据库中插入该文件的记录。
这是我的应用程序演示:http:/stormix.co/projects/megadrop/index
我可以使用普通文件输入轻松上传文件并在我的数据库中添加这些文件的记录(你可以在 link 我给了你)。
但是当我尝试用 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
第一次在这里提问,如有遗漏请见谅;告诉我,我会添加它
而且,我想知道如何 return 从 php 到 dropzone 的错误,因为我不知道如何:(
我的代码包含很多错误:
- $allowed_files 未定义:我将其设置为 ALL,如我的配置文件
- $targetpath 不正确,所以我修复了它
我用过:
header('Content-type: text/plain');
exit("My error");
显示错误,现在都已修复:D