我有一个前端表单(通过ajax提交)。我有一个按钮"添加文件" 。可以在每次点击按钮时浏览.Files。
我想上传多个文件并保存上传到文件夹的所有文件。
如何使用php / jquery做到这一点。请帮忙。
这是phtml
<form>
<div class="input-box"></input>
<div class="input-box">
<label for="addfile"><?php echo $this->__('Select File(s)');?></label><br />
<p>Upload any files that are necessary for your order by clicking on the Add File(s) button below.</p>
<input type="file" class="file" id="file" name="file[]" style="display: none;" onchange="fileSelected(this)" multiple="multiple">
<input type="button" class="button" id="btnAttachment" onclick="if(dataForm.validator && dataForm.validator.validate()){ openAttachment();}" value="Add File(s)"/>
</div>
<div id="select-file-form">
<div class="select-file-outer" id="select-file" style="display:none">
<label for="addfile"><?php echo $this->__('Select File(s)');?></label><br />
<p>Upload any files that are necessary for your order by clicking on the Add File(s) button below.</p>
<label for="files-for"><?php echo $this->__('Files for:');?></label><br />
<div class="select-file-company" id="select-file-company"></div>
<div class="select-file-name" id="select-file-name"></div>
<div class="select-file-phnmbr" id="select-file-phnmbr"></div>
<div class="select-file-email" id="select-file-email"></div>
<div class="select-file-det" id="select-file-det"></div>
<div>
<input type="button" class="button" id="btnAttachment" onclick="openAttachment()" value="Add File(s)"/>
</div>
<div>
<input type="button" class="button" id="btnUpload" onclick="callAjax()" value="Upload File(s)"/>
</div>
</div>
</div>
</form>
这是脚本
<script type="text/javascript">
var dataForm = new VarienForm('upload_form');
function openAttachment() {
document.getElementById('file').click();
}
function fileSelected(input){
var _size = input.files[0].size;
var fSExt = new Array('Bytes', 'KB', 'MB', 'GB'),
i=0;while(_size>900){_size/=1024;i++;}
var exactSize = (Math.round(_size*100)/100)+' '+fSExt[i];
var file_det ='<span id="filename">'+input.files[0].name+'</span><span id=exactsize>'+exactSize+'</span></br>';
//if(_size>0){
document.getElementById("upload_form_outer").style.display = "none";
document.getElementById("select-file").style.display = "block";
document.getElementById("select-file-company").innerHTML = document.getElementById("company").value;
document.getElementById("select-file-name").innerHTML = document.getElementById("f_name").value+" "+document.getElementById("l_name").value;
document.getElementById("select-file-phnmbr").innerHTML = "Phone Number:"+document.getElementById("phnmbr").value;
document.getElementById("select-file-email").innerHTML = "Email:"+document.getElementById("email").value;
document.getElementById("select-file-det").innerHTML += file_det;
// }
}
function callAjax(){
//var input = document.getElementById('attachment');
var formData = new FormData();
//formData.append("file", file);
var filename = document.getElementById("filename").innerHTML;
var companyname = document.getElementById("company").value;
var name = document.getElementById("f_name").value+" "+document.getElementById("l_name").value;
var phnumber = document.getElementById("phnmbr").value;
var email = document.getElementById("email").value;
var file = document.getElementById("select-file-det").innerHTML;
jQuery.ajax({
url: "<?php echo Mage::getUrl('uploads/index/fileupload'); ?>",
type: "POST",
data: formData,
contentType: 'multipart/form-data',
processData: false,
contentType: false,
success: function(res) {
},
error: function(res) {
}
});
}
</script>
这是php函数。
$post = $this->getRequest()->getPost();
$company = $this->getRequest()->getParam('company');
$name = $_POST['name'];
try{
for($i=0; $i<count($_FILES['file']['name']); $i++){
Mage::log('name'.$_FILES['file']['name'], null, 'test.log');
Mage::log('size'.count($_FILES['file']['name']), null, 'test.log');
$location = 'upload';
$path = Mage::getBaseDir() . DS .$location;
$file = new Varien_Io_File();
$files = glob($path . '*', GLOB_MARK);
foreach ($files as $f) {
if (! is_dir($f)) {
unlink($f);
}
}
$fileName = $_FILES['file']['name'];
$dirResult = $file->mkdir($path);
$uploader = new Varien_File_Uploader('file');
//$uploader->setAllowedExtensions(array('csv','zip','img','pdf','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setAllowCreateFolders(true);
$uploader->setFilesDispersion(false);
$uploader->save($path, $fileName);
}
}catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
}
但它没有进入for循环。我们如何在php函数中访问表单数据?
我试过这个链接。但$ _FILES [&#39; file&#39;] [&#39; name&#39;]会记录为null。 How to upload multiple files using PHP, jQuery and AJAX