移动多个上传的图像

时间:2013-08-28 17:21:07

标签: php

我使用for循环缩放了多个图像。现在我想将这些图像移动到一个文件夹中 我怎么能这样做?

move_uploaded_file($_FILES['image']['tmp_name'],'uploads/'.$_FILES['image']['name']);

我有这段代码,但只有一张图片可以放在uploads文件夹中,而其他图片则丢失了。

1 个答案:

答案 0 :(得分:0)

这段代码对我来说很好 form.html

<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>

upload.php的

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
$success = 0;
$fail = 0;

$uploads_dir = 'uploads';
$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)/",$ext)){
            $newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;
            if(move_uploaded_file($tmp_name, $newfile)){
                $success++;
            }else{
                echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
                $fail++;
            }
        }else{
            echo "Invalid Extension.\n";
            $fail++;
        }
    }
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;