在此教程中,使用经过修改的代码版本:http://sapphion.com/2012/06/12/keep-directory-structure-when-uploading/我已经能够将整个文件夹上载到Web服务器并保留其目录结构。然而。告诉用户上传成功完成的代码不起作用-似乎'echo'命令未执行。
这是代码。
PHP:
<?php
if(sizeof($_FILES) > 0)
$fileUploader = new FileUploader($_FILES);
class FileUploader{
public function __construct($uploads,$uploadDir='submissions/'){
// Split the string containing the list of file paths into an array
$paths = explode("###",rtrim($_POST['paths'],"###"));
// Loop through files sent
foreach($uploads as $key => $current)
{
// Stores full destination path of file on server
$this->uploadFile=$uploadDir.rtrim($paths[$key],"/.");
// Stores containing folder path to check if dir later
$this->folder = substr($this->uploadFile,0,strrpos($this->uploadFile,"/"));
// Check whether the current entity is an actual file or a folder (With a . for a name)
if(strlen($current['name'])!=1) {
// Upload current file
if($this->upload($current,$this->uploadFile)) {
echo "The files have been uploaded.";
//Email Storehouse
mail('email address', 'title', 'body', 'from');
}
else {
echo "Error";
}
}
}
}
private function upload($current,$uploadFile){
// Checks whether the current file's containing folder exists, if not, it will create it.
if(!is_dir($this->folder)){
mkdir($this->folder,0700,true);
}
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile)) {
return true;
}
else {
return false;
}
}
}
?>
注意:已编辑了删除敏感内容的电子邮件行,尽管它在有或没有电子邮件位的情况下均不起作用,但不应与“ echo”命令有所不同。
JavaScript可以保留目录结构(以防万一!)
window.onload = function(){
var output = document.getElementById('output');
// Detect when the value of the files input changes.
document.getElementById('files').onchange = function(e) {
// Retrieve the file list from the input element
uploadFiles(e.target.files);
// Outputs file names to div id "output"
output.innerText = "";
for (var i in e.target.files)
output.innerText = output.innerText + e.target.files[i].webkitRelativePath+"\n";
}
}
function uploadFiles(files){
// Create a new HTTP requests, Form data item (data we will send to the server) and an empty string for the file paths.
xhr = new XMLHttpRequest();
data = new FormData();
paths = "";
// Set how to handle the response text from the server
xhr.onreadystatechange = function(ev){
console.debug(xhr.responseText);
};
// Loop through the file list
for (var i in files){
// Append the current file path to the paths variable (delimited by tripple hash signs - ###)
paths += files[i].webkitRelativePath+"###";
// Append current file to our FormData with the index of i
data.append(i, files[i]);
};
// Append the paths variable to our FormData to be sent to the server
// Currently, As far as I know, HTTP requests do not natively carry the path data
// So we must add it to the request manually.
data.append('paths', paths);
// Open and send HHTP requests to upload-light.php
xhr.open('POST', "upload.php", true);
xhr.send(this.data);
window.alert("Files uploading.\n\nIMPORTANT: Please leave this page open for at least 10 minutes whilst the files upload!");
}
非常感谢您的帮助和/或建议!
Jason