PHP多文件上传相同图像上传5次而不是所有文件

时间:2013-10-29 19:00:40

标签: php file-upload

嘿伙计我真的在挣扎我的代码。当我尝试上传多张图片时,会上传相同的图片5次,而不是上传两张图片。任何帮助表示赞赏。 谢谢你的时间。

这是var_dumb($ _ FILES)

array(1) {
  ["userfile"]=>
  array(5) {
    ["name"]=>
    string(9) "test1.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpdDGCy2"
    ["error"]=>
    int(0)
    ["size"]=>
    int(1768037)
  }
}

这是我的上传功能:

if(count($_FILES['userfile'])){
    foreach($_FILES['userfile'] as $file){
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
echo '<pre>';
var_dump($_FILES);
echo '</pre>';
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
} 
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
    ||($_FILES["userfile"]["type"]=="image/jpeg")
    ||($_FILES["userfile"]["type"]=="image/png")
    ||($_FILES["userfile"]["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>";
           unlink ($_FILES['username']['tmp_name']);

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

以下是上传表单:

    <div class="container">
 <div class="row">
  <div class="col-md-6 col-md-offset-3">
          <h1>Upload a file</h1>
<form method="post" enctype="multipart/form-data" action="process.php">
<div id="filediv">
<div id="imagefiles">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile[]" type="file" id="userfile" multiple></label>
<label>Alt Text: <input name="alt" type="text"></label>
 </div>
 </div>
<br>

<input type="button" value="Add Another File" onclick="copyDiv()"/>
<input name="UploadFile" type="submit" />
</form>

4 个答案:

答案 0 :(得分:0)

而不是$_FILES['userfile']循环内部使用来自foreach的变量$file

同时使用var_dump上的$_FILES来检查其发送方式。

答案 1 :(得分:0)

如果一次上传多个文件,服务器很可能只接受一个文件。我有一次这个问题,我通过ajax文件上传修复它。 Ajax文件上传也非常有用,因为用户不必等待所有文件都传输到服务器。你可以用它。 https://github.com/blueimp/jQuery-File-Upload

答案 2 :(得分:0)

您可以尝试for loop而不是foreach .... as:

if(count($_FILES['userfile'])){
   for($i=0;$i<count($_FILES['userfile']);$i++) {
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
echo '<pre>';
var_dump($_FILES);
echo '</pre>';
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'][$i];
$tmpName  = $_FILES['userfile']['tmp_name'][$i];
$fileSize = $_FILES['userfile']['size'][$i];
$fileType = $_FILES['userfile']['type'][$i];
} 
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
    ||($_FILES["userfile"]["type"]=="image/jpeg")
    ||($_FILES["userfile"]["type"]=="image/png")
    ||($_FILES["userfile"]["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>";
           unlink ($_FILES['username']['tmp_name'][$i]);

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

答案 3 :(得分:0)

是的,我会考虑在这里使用JS / AJAX解决方案而不是php。由于加载时间和代码执行时的长页面刷新,上传多个文件对于php来说有点麻烦,没有真正的反馈给用户关于幕后发生的事情。

我曾多次使用uploadify并且效果很好。