通过 HTML 表单上传整个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4008406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload a whole directory through an HTML form
提问by Jayu
Is it possible to upload a folder with a file input in the browser?
是否可以在浏览器中上传带有文件输入的文件夹?
I searched and found out that this might be a browser limitation and that I might need to use a Java Applet or Flash.
我搜索并发现这可能是浏览器限制,我可能需要使用 Java Applet 或 Flash。
回答by Alan
It's becoming possible with use of webkitdirectory.
使用 webkitdirectory 成为可能。
<input type="file" webkitdirectory directory multiple />
Supported since Firefox 50, Chrome 30, Safari 11.1, Edge 14, but not on most mobile browsers as of 2019: https://caniuse.com/#feat=input-file-directory
自 Firefox 50、Chrome 30、Safari 11.1、Edge 14 起支持,但截至 2019 年在大多数移动浏览器上均不支持:https: //caniuse.com/#feat=input-file-directory
回答by Devil
Please Try This for upload the folder :
请尝试此上传文件夹:
<form method="post" enctype="multipart/form-data">
<input type="file" name="files[]" id="files" multiple="" directory="" webkitdirectory="" mozdirectory="">
<input class="button" type="submit" value="Upload" />
</form>
$count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
foreach ($_FILES['files']['name'] as $i => $name) {
if (strlen($_FILES['files']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['files']['tmp_name'][$i], 'folder/'.$name)) {
$count++;
}
}
}
}
`
`
回答by NOtherDev
回答by TRiG
It is possible to upload multiple files at a time, by drag and drop, without any browser plugins. This is a new development with HTML5 and javascript, so you'll probably need a fallback for older browsers.
可以通过拖放一次上传多个文件,无需任何浏览器插件。这是使用 HTML5 和 javascript 的新开发,因此您可能需要对旧浏览器进行回退。
It's called "HTML5 drag and drop". I've not used it yet, so I can't give you sample code, but searching for that phrase, and reading the linked Mozilla Blog article may give you some pointers.
它被称为“ HTML5 拖放”。我还没有使用它,所以我不能给你示例代码,但搜索该短语并阅读链接的 Mozilla 博客文章可能会给你一些指导。
回答by Spock
It doesn't seem possible to upload a folder by only using PHP but Javascript can detect folders so I solved it by doing these two steps:
似乎不可能仅使用 PHP 上传文件夹,但 Javascript 可以检测文件夹,所以我通过执行以下两个步骤解决了这个问题:
Create a Javascript function that reads the directory and files that will be uploaded and add this to a array(I called this Filestructure) that will be sent together with POST. For example:
{ 'foldername/': {'file1.txt','file2.txt}, 'foldername/folder2': {'foo.txt', 'bar.png'} }
创建一个 Javascript 函数来读取将要上传的目录和文件,并将其添加到一个将与 POST 一起发送的数组(我称之为 Filestructure)中。例如:
{ 'foldername/': {'file1.txt','file2.txt}, 'foldername/folder2': {'foo.txt', 'bar.png'} }
There is a similar function in Dropzone.js that already handles this that I had to modify (_addFilesFromDirectory() ). But you can create your own function to do this. See this https://stackoverflow.com/a/20431117/6760554if you need more help regarding this.
Dropzone.js 中有一个类似的函数,它已经处理了我必须修改的 (_addFilesFromDirectory())。但是您可以创建自己的函数来执行此操作。如果您需要更多帮助,请参阅此https://stackoverflow.com/a/20431117/6760554。
In Php you should first let your files be uploaded to a certain folder where they will be stored temporary. After your files has been uploaded you then need to pass your javascript array to your phpcode. There you need to iterate the array and create the folders and then move your uploaded files from the temporary folder to their respective location. For example:
$_filetree = $_POST['filetree']; function createFoldersAndMoveFiles($_filetree) { $nFolders = count($_filetree); foreach ($_filetree as $folder => $files) { createFolder($folder); moveFiles($files, $folder); } } function moveFiles($_files, $_folder) { $source = 'tmpuploads/'; $destination = 'mypath/'; $nFiles = count($_files); for($i = 0; $i < $nFiles; $i++) { $file = $_files[$i]; rename($source . $file, $destination .$_folder. '/' .$file); } } function createFolder($foldername) { $folders = explode("/", $foldername); $path = 'mypath/'; $nFolders = count($folders); for($i = 0; $i < $nFolders; $i++){ $newFolder = '/' . $folders[$i]; $path .= $newFolder; if (!file_exists($path) && !is_dir($path)) { mkdir($path); } } }
在 PHP 中,您应该首先让您的文件上传到某个文件夹,在那里它们将被临时存储。在您的文件上传后,您需要将您的 javascript 数组传递给您的 phpcode。在那里您需要迭代数组并创建文件夹,然后将上传的文件从临时文件夹移动到它们各自的位置。例如:
$_filetree = $_POST['filetree']; function createFoldersAndMoveFiles($_filetree) { $nFolders = count($_filetree); foreach ($_filetree as $folder => $files) { createFolder($folder); moveFiles($files, $folder); } } function moveFiles($_files, $_folder) { $source = 'tmpuploads/'; $destination = 'mypath/'; $nFiles = count($_files); for($i = 0; $i < $nFiles; $i++) { $file = $_files[$i]; rename($source . $file, $destination .$_folder. '/' .$file); } } function createFolder($foldername) { $folders = explode("/", $foldername); $path = 'mypath/'; $nFolders = count($folders); for($i = 0; $i < $nFolders; $i++){ $newFolder = '/' . $folders[$i]; $path .= $newFolder; if (!file_exists($path) && !is_dir($path)) { mkdir($path); } } }
I hope this helps.
我希望这有帮助。
回答by Joseeph
to upload folder in php, use these steps
在php中上传文件夹,使用这些步骤
<form id="form1" action="myCurrent.php" method="post">
<label>Upload All Files From Folder</label> <br/>
<input id="input" name="input[]" type="file" multiple webkitdirectory >
<div id="errorBlock" class="help-block"></div> <br/>
<input type="submit" name="btnDesFolder" id="btnDesFolder" value="send file" />
</form>
<?php
if(isset($_POST['btnDesFolder'])){
$myFiles = $_POST['input-folder-2'];
if(isset($_POST['input-folder-2'])){
$files = scandir("path/to/your/folder");
$oldfolder = "path/to/your/folder/";
$newfolder = "path/to/new/folder";
foreach($files as $fname) {
if($fname != '.' && $fname != '..' && $fname != 'index.php') {
rename($oldfolder.$fname, $newfolder.$fname);
}
}
}
}
?>
回答by Cholthi Paul Ttiopic
You can archive the directory with something like tar
and then upload it as a one file.But be careful, you might exceed php upload max which is by default set at 2MB. This is configurable though.
您可以使用类似的方式归档目录,tar
然后将其作为一个文件上传。但要小心,您可能会超过默认设置为 2MB 的 php upload max。虽然这是可配置的。