1

我终于取得了一些进展。现在,当我上传 2 张图片时,它会被放入我的数组中。我附上了我的vardumb。但是文件实际上并没有上传?有任何想法吗?

array(5) {
  ["name"]=>
  array(2) {
    [0]=>
    string(11) "1bTUWI3.jpg"
    [1]=>
    string(11) "1T1NBmd.jpg"
  }
  ["type"]=>
  array(2) {
    [0]=>
    string(10) "image/jpeg"
    [1]=>
    string(10) "image/jpeg"
  }
  ["tmp_name"]=>
  array(2) {
    [0]=>
    string(14) "/tmp/phpgB2bAe"
    [1]=>
    string(14) "/tmp/phpg8ZZk1"
  }
  ["error"]=>
  array(2) {
    [0]=>
    int(0)
    [1]=>
    int(0)
  }
  ["size"]=>
  array(2) {
    [0]=>
    int(671869)
    [1]=>
    int(352029)
  }
}
    function uploadFile()

} 
$files = array();
$fdata = $_FILES['userfile'];
if (is_array($fdata["name"])){
    for ($i = 0; $i < count($fdata['name']); ++$i) {
        $files[] = array(
        'name' => $fdata['name'][$i],
        'tmp_name' => $fdata['tmp_name'][$i],
        'size' => $fdata['size'][$i],
        'filetype' => $fdata['type'][$i],
        );
    }

    echo '<pre>';
var_dump($fdata);
echo '</pre>';
$username=$_SESSION['name'];
$alt=mysqli_real_escape_string($conn, $_POST['alt']);

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($fdata['type'] == "image/gif")
    ||($fdata['type']=="image/jpeg")
    ||($fdata['type']=="image/png")
    ||($fdata['type']=="image/pjpeg")
    && in_array($extension, $allowedExts)))
    {
        $fp = fopen($tmpName, 'r');
        $content =fread($fp, filesize($tmpName));
        $SourceImage = imagecreatefromstring($content);
        $SourceWidth = imagesx($SourceImage);
        $SourceHeight=imagesy($SourceImage);
        $DestWidth=100;
        $DestHeight=130;
        if ($SourceHeight> $SourceWidth)
        {$ratio = $DestHeight / $SourceHeight;
        $newHeight = $DestHeight;
        $newWidth = $sourceWidth * $ratio;
        }
        else
        {
            $ratio = $DestWidth / $SourceWidth;
            $newWidth = $DestWidth;
            $newHeight = $SourceHeight * $ratio;
        }
        $DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
        ob_start();
        imagejpeg($DestinationImage);
        $BinaryThumbnail = ob_get_contents();
        ob_end_clean();
        $thumb = addslashes($BinaryThumbnail);
        $content = addslashes($content);
        fclose($fp);
        $fp      = fopen($tmpName, 'r');
        $content = fread($fp, filesize($tmpName));
        $content = addslashes($content);
        fclose($fp);

          mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed'); 
           echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";


    }else{ 
           echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
    }

}
}

我仍然陷入我的回声错误。

我的表格看起来像这样。

      <h1>Upload a file</h1>
<form method="post" enctype="multipart/form-data" action="process.php">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile" type="file" id="userfile"></label>
<br>
<label>Alt Text: <input name="alt" type="text"></label>
<input name="UploadFile" type="submit" />
</form>

我的 vardump 看起来像这样

array(1) {
  ["userfile"]=>
  array(5) {
    ["name"]=>
    array(1) {
      [0]=>
      string(17) "Desert - Copy.jpg"
    }
    ["type"]=>
    array(1) {
      [0]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      [0]=>
      string(14) "/tmp/phpIpCoQ3"
    }
    ["error"]=>
    array(1) {
      [0]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      [0]=>
      int(845941)
    }
  }
}
4

1 回答 1

0

首先确认userfile不是username

和检查error一样loop

// check the userfile array is set and it is not empty
if(isset($_FILES["userfile"]) and !empty($_FILES["userfile"]))
{
    // loop for each file to upload
    foreach ($_FILES['userfile'] as $file)
    {
       // check for error and size
       if (($file["error"] == 0) && ($file['size'] > 0))
       {
          // your remaning code

而在HTML中使用类似array的,file names

<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
于 2013-10-30T13:04:56.067 回答