好吧,我可以成功上傳1個文件,使其在主文件夾內創建一個新文件夾。然後將文件放在那裏。如何上傳多個文件,創建文件夾並將文件放置在那裏PHP
但是我很難做多個文件到同一個文件夾。
我要上傳2個文件,看起來像這樣:
文檔/ 1 /然後將文件在這裏。
是什麼樣子:
文檔/ 1
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:<br/>
<input type="file" name="fileToUpload" id="fileToUpload"><br/>
<input type="file" name="fileToUpload" id="fileToUpload"><br/>
<input type="submit" value="Upload Image" name="submit">
</form>
PHP:
<?php
$number = 1;
$target_dir = "docs/";
$new = mkdir($target_dir . $number . "/");
$target_file = $target_dir . $number . "/";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
foreach($_FILES['fileToUpload']['name'] as $file => $uploaded_file){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file . $uploaded_file);
}
}
?>
任何想法?
編輯工作:
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="submit" value="Send" />
</form>
$target_dir = "docs/";
$dir=glob($target_dir."/*",GLOB_ONLYDIR);
$number = count($dir) + 1;
$new = mkdir($target_dir . $number . "/");
$target_file = $target_dir . $number . "/";
foreach ($_FILES["pictures"]["name"] as $key => $Name)
{
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, $target_file . "$name");
}
'我有麻煩了'麻煩怎麼表現出來? - 閱讀:更多信息pls; (也是你的文件輸入具有相同的名稱) – birdspider
你有2個輸入字段具有相同的名稱。 –
對不起,我打算取出$ new =即使它仍然創建1文件夾。 和我知道我有同樣的名字,我雖然這就是爲什麼我會通過他們的foreach? – user3620531