2

所以我尝试在我的网站中使用Fine-uploader (Minimal jQuery Demo) 将一些图片上传到网络服务器上传文件夹。但是上传总是失败,我不知道为什么。

当我单击“上传文件”并选择一个文件时,它开始上传(进度条开始填充),当它达到 100% 时,它显示“上传失败”并在检查元素中给出此错误:( 更大尺寸的图像)

这是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";
  }
}

?>
4

3 回答 3

2

您没有返回有效的 JSON 响应。一个简单的方法是使用json_encode.

将您当前的回显线放入一个数组中,并在使用后回显该数组json_encode,以获得有效的 JSON 响应。

于 2013-07-26T19:04:36.173 回答
0

尝试将您的权限设置为 777

于 2013-08-01T02:19:57.933 回答
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格式。上面提供的代码有注释。

于 2013-07-31T21:34:28.487 回答