代码不上传带有ajax文件上传的图像

时间:2015-11-03 11:28:33

标签: php ajax dropzone.js

我正在尝试将图像上传到服务器,代码工作正常,提交按钮文件正确上传并提供正确的输出。 如下

<?php
//header('Content-type: application/json');
// Upload and Rename File
include 'reqFunctions.php';
$timestamp1=time();
$uploadDir="C:/test/";
$userId=20;
$userFolder=null;
$userId=$_POST["userId"];
echo "user id is " .$userId;
$userFolder=md5($userId);
$newUploadDir=$uploadDir."/".$userFolder."/profile/";
//if(isLogin()==false){
foreach($_FILES as $value){

$filename = $value["name"];
 echo "file name  ".$filename;
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    echo "file detail is ".$filename."<br>";
    echo "file extension is ".$file_ext."<br>";
    echo "file size  is ".$filesize."<br>";
    $allowed_file_types = array('.jpg','.gif','.mpeg'); 
    if (in_array($file_ext,$allowed_file_types) && ($filesize < 20000000)){
      if (!file_exists($uploadDir."/".$userFolder)) {
             mkdir($uploadDir."/".$userFolder, 0777, true);
     }
      if (!file_exists($uploadDir."/".$userFolder."/profile")) {
             mkdir($uploadDir."/".$userFolder."/profile", 0777, true);
     }
      $newfilename=generateRandomString(10)."_".$timestamp1."_".generateRandomString(10).$file_ext;
    //echo "new file name is ".$newfilename."<br>";

     if (file_exists($newUploadDir. $newfilename))
        {
            // file already exists error
            //echo "You have already uploaded this file.<br>";
        }
        else
        {       
            move_uploaded_file($_FILES["file"]["tmp_name"], $newUploadDir. $newfilename);
            //echo "File uploaded successfully. <br>";      
        }
    }
    elseif (empty($file_basename))
    {   
        // file selection error
        //echo "Please select a file to upload.<br>";
    }
    elseif ($filesize > 20000000)
    {   
        // file size error
        //echo "The file you are trying to upload is too large.";
    }
    else
    {
        // file type error
        //echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
        unlink($_FILES["file"]["tmp_name"]);
    }
    $path= $newUploadDir. $newfilename;
    }
    echo ($path);
    //echo(json_encode($path));
    /*}else{
    echo(json_encode("Please login"));
    }*/
 ?>

html文件

<form action="fileUpload2.php" enctype="multipart/form-data" method="post">
<input id="file" name="file" type="file" />
<input id="file" name="userId" type="text"  value="2"/>
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>

以上代码正常运作

现在我用

更改了html代码
<form action="#" class="dropzone" method="post"></form>
<script>
    $(".dropzone").dropzone({
        paramName: "file",
        maxFilesize: 20,
        url: 'fileUpload2.php',
        uploadMultiple: true,
        parallelUploads: 5,
        dataType: 'json',
        maxFiles: 10,
        acceptedFiles: "image/*,audio/*,video/*",
        init: function () {
          this.on('sending', function(file, xhr, formData){
            formData.append('userId', 155);
        });
              this.on("success", function (file, responseText) {
                console.log("l path " + responseText);
                console.log(file);
                console.log("---------------------------------");
               });
            }
    });
    </script>

不在服务器上传图像 在控制台上显示

l path user id is 155
( ! ) Notice: Array to string conversion in C:\wamp\www\fileupload\fileUpload2.php on line 17
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
file name Array
( ! ) Warning: strripos() expects parameter 1 to be string, array given in C:\wamp\www\fileupload\fileUpload2.php on line 18
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
2   0.0009  157304  strripos ( )    ..\fileUpload2.php:18

( ! ) Warning: substr() expects parameter 1 to be string, array given in C:\wamp\www\fileupload\fileUpload2.php on line 18
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
2   0.0011  157320  substr ( )  ..\fileUpload2.php:18

( ! ) Warning: strripos() expects parameter 1 to be string, array given in C:\wamp\www\fileupload\fileUpload2.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
2   0.0012  157392  strripos ( )    ..\fileUpload2.php:19

( ! ) Warning: substr() expects parameter 1 to be string, array given in C:\wamp\www\fileupload\fileUpload2.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
2   0.0014  157384  substr ( )  ..\fileUpload2.php:19

( ! ) Notice: Array to string conversion in C:\wamp\www\fileupload\fileUpload2.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
file detail is Array
file extension is 

( ! ) Notice: Array to string conversion in C:\wamp\www\fileupload\fileUpload2.php on line 23
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0
file size is Array

( ! ) Notice: Undefined variable: newfilename in C:\wamp\www\fileupload\fileUpload2.php on line 62
Call Stack
#   Time    Memory  Function    Location
1   0.0005  152104  {main}( )   ..\fileUpload2.php:0

如何解决上述问题?

修改

var_dump($_FILES);

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
  'file' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=5)</i>
      'name' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=1)</i>
          0 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'birds-111v.jpg'</font> <i>(length=14)</i>
      'type' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=1)</i>
          0 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>
      'tmp_name' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=1)</i>
          0 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'C:\wamp\tmp\phpDF89.tmp'</font> <i>(length=23)</i>
      'error' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=1)</i>
          0 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
      'size' <font color='#888a85'>=&gt;</font> 
        <b>array</b> <i>(size=1)</i>
          0 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>197523</font>

1 个答案:

答案 0 :(得分:0)

那是因为文件名现在是:

$filename = $value["name"][0];