0

わかりました、私は答えを探しましたが、助けるものは何も見つかりませんでした. while ループと forloop を使用してみましたが、アップロードされるファイルは 1 つだけです。これが私のコードです。

形:

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

}
}

持っていたコードの半分は必要ないことに気付きました。今、私は1つの画像をアップロードしていますが、両方ではありません。

4

2 に答える 2

0

$_FILES['userfile']とを実行することで$_FILES['file']、アップロード フィールドを介してアップロードされたファイル (それぞれuserfilefile) のみを参照しています。 $_FILESは連想配列なので、次のようにする必要があります

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

$_FILES["userfile'"]また、約 12 行下にあることに注意してください。余分な'ものはその線を壊します。

詳細については、PHP ヘルプ ファイルを参照してください。

于 2013-10-28T20:32:28.033 に答える