Reputation: 610
So I'm trying to use fine-uploader(Minimal jQuery Demo) in my website to upload some pictures to the webserver Upload folder. However the upload is always failing and I have no idea why.
When I click "upload a file" and choose a file it starts uploading (progressbar starts filling) and when it gets to 100% it says "Upload failed" and gives this error in inspect element:
(Bigger size image)
Here is 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>
And 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";
}
}
?>
Upvotes: 2
Views: 1609
Reputation: 524
<?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 );
?>
First thing is remove it from the loop. Even though it supports multi-upload it only sends 1 per file. See in the fire bug. Then i used absolute path of the folder and then converted the response into JSON
format. The code provided above has comments.
Upvotes: 0
Reputation: 5382
You're not returning a valid JSON response. An easy way to do this would be to use json_encode
.
Put your current echo lines in an array, and echo that array after using json_encode
, to get a valid JSON response.
Upvotes: 2