所以我试图在我的网站上使用fine-uploader(Minimal jQuery Demo)将一些图片上传到webserver上传文件夹。然而,上传总是失败,我不知道为什么。
当我点击“上传文件”并选择一个文件时,它开始上传(进度条开始填充),当它达到100%时,它会显示“上传失败”并在inspect元素中给出此错误: (Bigger size image)
这是upload.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fine Uploader - jQuery Wrapper Minimal Demo</title>
<link href="css/fineuploader-3.6.3.css" rel="stylesheet">
</head>
<body>
<div id="fine-uploader"></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/jquery.fineuploader-3.6.3.js"></script>
<script>
function createUploader() {
var uploader = new qq.FineUploader({
element: document.getElementById('fine-uploader'),
request: {
endpoint: 'upload_file.php'
}
});
}
window.onload = createUploader;
</script>
</body>
</html>
和upload_file.php:
<?php
for($i=0; $i<count($_FILES['file']['type']); $i++) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
if ((($_FILES["file"]["type"][$i] == "image/gif")
|| ($_FILES["file"]["type"][$i] == "image/jpeg")
|| ($_FILES["file"]["type"][$i] == "image/jpg")
|| ($_FILES["file"]["type"][$i] == "image/pjpeg")
|| ($_FILES["file"]["type"][$i] == "image/x-png")
|| ($_FILES["file"]["type"][$i] == "image/png"))
&& ($_FILES["file"]["size"][$i] < 50000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"][$i] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"][$i] . "<br>";
echo "Type: " . $_FILES["file"]["type"][$i] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][$i] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"][$i] . "<br>";
if (file_exists("upload\\" . $_FILES["file"]["name"][$i]))
{
echo $_FILES["file"]["name"][$i] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"][$i],
"\\upload\\" . $_FILES["file"]["name"][$i]);
echo "Stored in: " . "upload\\" . $_FILES["file"]["name"][$i];
}
}
}
else
{
echo "Invalid file";
}
}
?>
答案 0 :(得分:2)
您没有返回有效的JSON响应。一种简单的方法是使用json_encode
。
将当前的回显线放在一个数组中,并在使用json_encode
后回显该数组,以获得有效的JSON响应。
答案 1 :(得分:0)
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["qqfile"]["name"]);
$extension = end($temp);
/**
* This is just additional
*
*/
$folder = dirname( __FILE__) ."/testuploads";
if( !is_dir( $folder ))
{
@mkdir( $folder , 777 );
}
/**
* I remove the for loop since we are expecting a single file.
* The uploader supports multi uploads but what it exactly does is sending multiple request. See in the firebug.
* 1 request per file.
*/
if ((($_FILES["qqfile"]["type"] == "image/gif")
|| ($_FILES["qqfile"]["type"] == "image/jpeg")
|| ($_FILES["qqfile"]["type"] == "image/jpg")
|| ($_FILES["qqfile"]["type"] == "image/pjpeg")
|| ($_FILES["qqfile"]["type"] == "image/x-png")
|| ($_FILES["qqfile"]["type"] == "image/png"))
&& ($_FILES["qqfile"]["size"] < 50000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["qqfile"]["error"] > 0)
{
$msg['return_code'] = "Return Code: " . $_FILES["qqfile"]["error"] . "<br>";
}
else
{
/*
* I put the extra messages into array so we can display it later in json format.
*/
$msg['upload'] = "Upload: " . $_FILES["qqfile"]["name"] . "<br>";
$msg['type'] = "Type: " . $_FILES["qqfile"]["type"] . "<br>";
$msg['size'] = "Size: " . ($_FILES["qqfile"]["size"] / 1024) . " kB<br>";
$msg['temp_file'] = "Temp file: " . $_FILES["qqfile"]["tmp_name"] . "<br>";
if( $_FILES["qqfile"]["name"] <> "" )
if (file_exists($folder."/" . $_FILES["qqfile"]["name"]))
{
$msg['error_msg'] = $_FILES["qqfile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["qqfile"]["tmp_name"],
$folder."/" . $_FILES["qqfile"]["name"]);
$msg['success_msg'] = "Stored in: " .$folder."/" . $_FILES["qqfile"]["name"];
/**
* Since we get here we have to tell your handler that the process is success
*/
$msg['success'] = "true";
}
}
}
else
{
$msg['error_msg'] = "Invalid file";
$msg['success'] = "false";
}
/**
* Display the result in json format.
*/
print json_encode( $msg );
?>
首先是从循环中删除它。即使它支持多上传,它也只发送1个文件。见火虫。然后我使用文件夹的绝对路径,然后将响应转换为JSON
格式。上面提供的代码有评论。
答案 2 :(得分:0)
尝试将您的权限设置为777