我尝试实现文件上传器+ mysqli数据插入。
该脚本使用PHP函数move_uploaded_file
和mysqli_connect
,并在move_uploaded_file
返回成功时完美运行。
但是当move_uploaded_file
因连接问题或用户取消而失败时,处理mysqli请求大约需要12分钟。
问题是:
如果很多用户取消他们的上传,我的服务器将会加载待处理的请求吗?
我正在尝试立即处理第一个Mysqli请求,它有效,
但是只要我添加move_uploaded_file
函数并导致客户端失败,它的行为就像描述的那样。
到目前为止,这是PHP代码:
if (isset($_POST['captcha']) AND ($_POST['captcha'] == '*')){
$id_link = mysqli_connect($db_host,$db_user,$db_pass,$db_name) or die("Error " . mysqli_error($id_link));
$currenttime = time();
$uniqueId = rand(1000, 9999);
$time = $currenttime . 'pr' . $uniqueId ;
$name = addslashes($_POST['name']);
$surname = addslashes($_POST['surname']);
$email = addslashes($_POST['email']);
$fileName = addslashes($_FILES["file"]["name"]);
$query = "INSERT INTO `mydb`.`mytable` (`date`, `name`, `surname`, `email`, `file`, `uploaddone`) VALUES ('". $time ."', '" . $name . "', '" . $surname . "', '" .$email . "', '" . $fileName . "', '0');";
$result = $id_link->query($query) or die("Error in the consult.." . mysqli_error($id_link));
if (isset($_FILES['file']) AND $_FILES['file']['error'] == 0){
$fileTmpLoc = $_FILES["file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file"]["type"]; // The type of file it is
$fileSize = $_FILES["file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file"]["error"]; // 0 for false... and 1 for true
if ($fileSize <= 200000000){
$infosfile = pathinfo($fileName);
$extension_upload = $infosfile['extension'];
$extensions_allowed = array('jpg', 'jpeg', 'gif', 'png' , 'mov' , 'avi' , 'mp4' , 'mpeg4' , 'MPEGPS' , 'MPEG4' , 'FLV' , 'flv' , 'WMV' , 'wmv' , '3GPP' , '3gpp' , 'WebM' , 'mp2' , 'mpeg2' , 'MPG' , 'm4v' , 'pdf' , 'MOV' , 'AVI' , 'MP4' , 'M4V' , 'PNG' , 'PDF' , 'JPEG' , 'JPG' , 'GIF', 'tif' , 'tiff' , 'TIF' , 'TIFF','mkv' , 'dv' , 'mts' , 'qt' , 'ogv' , 'ts' , 'vob' , 'mpeg' , 'mpe' , 'f4v' , 'dat' , 'divx' , 'asf' , '3g2' , '3gp', 'MKV' , 'DV' , 'MTS' , 'QT' , 'OGV' , 'TS' , 'VOB' , 'MPEG' , 'MPE' , 'F4V' , 'DAT' , 'DIVX' , 'ASF' , '3G2' , '3GP');
if (in_array($extension_upload, $extensions_allowed)){
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
//check if file exist and rename it
$newname = $fileName;
$filenamecheck = 'upload/' . $newname;
if ($pos = strrpos($fileName, '.')) {
$name = substr($fileName, 0, $pos);
$ext = substr($fileName, $pos);
} else {
$name = $fileName;
}
$counter = 2;
while (file_exists($filenamecheck)) {
$fileName = $name .'-'. $counter . $ext;
$filenamecheck = 'upload/' . $fileName;
$counter++;
}
if(move_uploaded_file($fileTmpLoc, "upload/$fileName")){
$query= "UPDATE `mydb`.`mytable` SET `file` = '" . $fileName . "',`uploaddone` = '1' WHERE `mytable`.`date` =" . $time;
$result = $id_link->query($query) or die("Error in the consult.." . mysqli_error($id_link));
die('{"error" : false, "message" : "Congratulations your upload is complete<br>you can now<br>"}');
} else {
die('{"error" : true, "message" : "Sorry Network Issue"}');
}
}else{
die('{"error" : true, "message" : "Sorry wrong file format"}');
}
}else{
die('{"error" : true, "message" : "File too big"}');
}
}
}else{
die('{"error" : true, "message" : "Please answer the security question before uploading"}');
}
我尝试了很多不同的解决方案(php标头删除缓存,关闭并重新打开msqli请求)但到目前为止没有任何工作,
如果你们有任何想法,那么信条会非常好,
非常感谢!