0

次のコードを使用して、サーバーに複数の画像をアップロードしたいと思います。

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input id="picture_01" name="userfile['01']" tabindex="auto" type="file">
<input id="picture_02" name="userfile['02']" tabindex="auto" type="file">
  <input id="picture_03" name="userfile['03']" tabindex="auto" type="file">
<input id="picture_04" name="userfile['04']" tabindex="auto" type="file">
<input id="picture_05" name="userfile['05']" tabindex="auto" type="file">
<input id="picture_06" name="userfile['06']" tabindex="auto" type="file">  
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

および upload_file.php

 <?php

$foldername = "anotherfolder";

$userfile = array(
            '01' => 'hello',
            '02' => 'bye',
            '03' => 'likka',
            '04' => 'pippa',
            '05' => 'laptop',
            '06' => 'cow06',
            '07' => 'cow07',
            '08' => 'cow08',
            '09' => 'cow09',
            '10' => 'cow10',            
            );

            echo $userfile ['01'];

foreach ($userfile as $keys => $values); 

//Upload Images
$success = 0;
$fail = 0;
$uploads_dir = "temp_images";
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
        $name = $_FILES["userfile"]["name"][$key];
        $uploadfile = "$uploads_dir/$name";
        $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
        if (preg_match("/(jpg|gif|png|bmp|jpeg)/",$ext)){
            $newfile = "$uploads_dir/"."$values".".".$ext;
            if(move_uploaded_file($tmp_name, $newfile)){
                           }else{
                echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
            }
        }else{
            echo "Invalid Extension.\n";
            $fail++;
        }
    }
}

?>

誰かが最初の入力で最初の画像をアップロードすると名前が「hello」に変更され、2 番目の入力でアップロードされた場合は名前が「bye」に変更されることを望みます。2 つの写真がアップロードされた場合、最初の写真の名前は「hello」、2 番目の写真の名前は「bye」に変更されます。

解決策を探してみましたが、うまくいきませんでした。何が悪いのか理解できません。ファイルは、画像がどの入力からアップロードされたかに関係なく、配列内の最新の値である「cow10」という順番で名前が付けられています。

ヘルプや提案はありますか?

4

2 に答える 2

0

私のコードは機能しています。要件に応じて変更する必要があります。テスト用に 2 つの .jpg ファイルをアップロードし、1.jpg と 2 つの写真 .jpg として保存しました。完了するのは私の仕事ではなく、あなたを助けることなので、コードを完成させません。以下はファイルtesting.phpのコードです(このフォームと同じファイルにアップロードされています。コードを更新しました。私のコンピューターで動作しています。このような問題は自分で解決してみてください。

<?php

$array = array("'01'"=>"1.jpg","'02'"=>"2.jpg");
$i=0;
if(!empty($_FILES["userfile"]["error"])){
    foreach ($_FILES["userfile"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
            $name = $array[$key];echo $key;
            move_uploaded_file($tmp_name, $name);
            $i++;
        }
    }
}
?>
<html>
<body>

<form action="testing.php" method="post" enctype="multipart/form-data">
<input id="picture_01" name="userfile['01']" tabindex="auto" type="file">
<input id="picture_02" name="userfile['02']" tabindex="auto" type="file">
  <input id="picture_03" name="userfile['03']" tabindex="auto" type="file">
<input id="picture_04" name="userfile['04']" tabindex="auto" type="file">
<input id="picture_05" name="userfile['05']" tabindex="auto" type="file">
<input id="picture_06" name="userfile['06']" tabindex="auto" type="file">  
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>
于 2013-10-04T23:29:54.227 に答える