好的,我可以成功上传1个文档,让它在主文件夹中创建一个新文件夹。然后将文件放在那里。
但是我无法将多个文件写入同一个文件夹。
我想上传2个文档,如下所示:
docs / 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");
}
答案 0 :(得分:1)
首先将文件输入更改为(注意所有元素的id应该是唯一的,以避免javascript问题):
<input type="file" name="fileToUpload[]" id="fileToUpload_0">
由于您要上传文件数组。
foreach($_FILES['fileToUpload'][$name] as $index=>$uploaded_file) {
move_uploaded_file(
$_FILES['fileToUpload']['tmp_name'][$index],
$target_file.$uploaded_file);
}
更新:在语言和文件处理逻辑中暂时混合。