0

好的,伙计们,我已经寻找答案,但找不到任何帮助。我尝试使用while循环和forloop,但只有一个文件被上传。这是我的代码。

形式:

<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>

这是上传功能:

$alt=mysqli_real_escape_string($conn, $_POST['alt']);
foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'][$key];
$tmpName  = $_FILES['userfile']['tmp_name'][$key];
$fileSize = $_FILES['userfile']['size'][$key];
$fileType = $_FILES['userfile']['type'][$key];
} else{
    echo"error";
}

$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>";
    }

}
}

我意识到我不需要一半的代码。现在我上传了一张图片,但不再同时上传了。

4

2 回答 2

0

通过$_FILES['userfile']$_FILES['file'],您仅指通过具有这些名称的上传字段上传的文件(userfilefile,分别)。 $_FILES是一个关联数组,所以你应该做类似的事情

foreach ($_FILES as $fieldName => $fileProperties) {
    // do something
}

另外,请注意您有$_FILES["userfile'"]大约十几行。额外的'会打破这条线。

有关更多信息,请参阅PHP 帮助文件

于 2013-10-28T20:32:28.033 回答
0

我可能错了,我不确定我是否曾经编写过 $_FILE 数组....但我认为问题就在这里

 while ($_FILES["userfile'"]["tmp_name"][$counter]){

除了额外的报价,我不认为数据是这样存储的......你可以尝试做 print_r($_FILES)

有可能

$_FILES["userfile"][$counter]["tmp_name"]

但我实际上怀疑这是否会奏效。只需循环通过 $_FILE 数组本身来处理动态数量的文件......这是我使用的函数......

public static function upload($file, $accountDirectory, $uploadDirectory)
{
    $return = array();
    if(!file_exists($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
        $return["result"] = false;
    }
    if($file['error'] != UPLOAD_ERR_OK) {
         $return["result"] = false; 
          switch($file['error']){
            case 0: //no error; possible file attack!
              echo "There was a problem with your upload.";
              break;
            case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
              echo "The file you are trying to upload is too big.";
              break;
            case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
              echo "The file you are trying to upload is too big.";
              break;
            case 3: //uploaded file was only partially uploaded
              echo "The file you are trying upload was only partially uploaded.";
              break;
            case 4: //no file was uploaded
              echo "You must select an image for upload.";
              break;
            default: //a default error, just in case!  :)
              echo "There was a problem with your upload.";
              break;            
          }         
    } else {
        $newFileName = preg_replace("/[^a-zA-Z0-9-.]/", "", $file["name"]);
        $uploadLocation = $accountDirectory . $uploadDirectory . $newFileName;
        while (file_exists($uploadLocation)) {
            $uploadLocation = $accountDirectory . $uploadDirectory . time() . $newFileName;
        }
        if (move_uploaded_file($file["tmp_name"],$uploadLocation)==true) {
            $return["file"] = str_replace($accountDirectory, "/",$uploadLocation);
            $return["result"] = true;
        } else {
            $return["result"] = false;
        }
    }
    return $return;
}
于 2013-10-28T20:12:25.377 回答