我不知道为什么这段代码不起作用
它将自己限制为2次上传 如果我选择四张图片,如果我选择2则上传1只会上传2张 它有多次运行而且我不知道
<?php
echo 'hey';
?>
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
Year:<br>
<input type="text" name="year">
</li>
<li>
Make:<br>
<input type="text" name="make">
</li>
<li>
Image1: <input name="userfile[]" type="file" /><br /></li>
<li>Image2: <input name="userfile[]" type="file" /><br /></li>
<li>Image3: <input name="userfile[]" type="file" /><br /></li>
<li>Image4: <input name="userfile[]" type="file" /><br /></li>
<input type="submit" value="Upload" />
</form>
</ul>
</form>
<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
$success = 0;
$fail = 0;
$uploads_dir = 'images';
$count = rand(5, 7000);
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;