我有一个PHP网站上的jQuery文件上传插件。jQuery文件上传插件:如何上传到子文件夹?
我想知道是否有可能将文件上传到动态命名的子文件夹而不是全部进入相同的上传文件夹?
原因是我需要一个单独的文件夹来上传由用户在网站上创建的每个“项目”中的文件。例如。当用户创建项目时,他们为该项目上传的所有内容都将转到/ uploads/{$ project_uid}/{$ file_name}
我希望我已经正确地解释了自己,如果有人能帮助我,在这里。
谢谢!
我有一个PHP网站上的jQuery文件上传插件。jQuery文件上传插件:如何上传到子文件夹?
我想知道是否有可能将文件上传到动态命名的子文件夹而不是全部进入相同的上传文件夹?
原因是我需要一个单独的文件夹来上传由用户在网站上创建的每个“项目”中的文件。例如。当用户创建项目时,他们为该项目上传的所有内容都将转到/ uploads/{$ project_uid}/{$ file_name}
我希望我已经正确地解释了自己,如果有人能帮助我,在这里。
谢谢!
Firt,明显的应当说明:您不能实际上设置上传目的地使用JavaScript/jQuery的/ jWhateverPlugin(即从客户端),一个明显的安全原因。
但你可以传递信息到服务器端引擎(在你的情况下,PHP),它可能用它来管理上传的实际存储。
存在各种工具包可以帮助您,例如最初开始使用的blueimp的jQuery File Upload或Uploadify,Benno首次提升并似乎符合您的要求。
所以你要做的是定制客户端大小和服务器端脚本来实现传递目录变量并使用它们来定义存储位置。重基础上,Uploadify documentation
,并使用你的project_uid
变量,这对子级是这样的:
在客户端(JavaScript + jQuery的+ Uploadify):
var project_uid;
// populate project_uid according to your needs and implementation
// befor using uploadify
$('#file_upload').uploadify({
'method' : 'post',
// pass your variable to the server
'formData' : { 'project_uid' : project_uid },
// optional "on success" callback
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
});
和服务器上 - (PHP + Uploadify):
// Set $someVar to 'someValue'
$untrustedProjectUid = $_POST['project_uid'];
// Remember to check heavily the untrusted received variable.
// Let's pretend you checked it, it passe your tests, so you
// initialized a trusted var with it
$trustedProjectUid = ensureSecure($untrustedProjectUid);
$targetFolder = '/uploads/' . $trustedProjectUid . '/' ; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // Put you allowed file extensions here
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo $targetFolder . '/' . $_FILES['Filedata']['name'];
} else {
echo 'Invalid file type.';
}
}
您使用哪个插件? Uploadify?如果您可以控制在JavaScript中设置要上传到哪个路径,则应该能够根据其他变量动态更改该路径,即项目ID /文件名。 – Benno
你不能直接上传到你喜欢的任何文件夹,我想他们都先进入一个普通的临时文件夹,然后你使用move_upload_file。 – Anonymous
该插件被称为jQuery文件上传,可以在这里找到:http://blueimp.github.com/jQuery-File-Upload/ – user1378715