3
這個問題是關係到我以前的question,我會創造使用模板文件管理選項卡形式。模板文件的內容爲:上傳多個文件Varien_File_Uploader
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend">Images</h4>
</div>
<div class="fieldset">
<div class="hor-scroll">
<table class="form-list container">
<tr class="wrapper-tr">
<td class="value">
<input type="file" name="images[]"/>
</td>
<td class="label">
<span class="remove">Remove</span>
</td>
</tr>
</table>
<input type="button" class="add" value="Add Image"/>
</div>
</div>
</div>
<script>
jQuery(document).ready(function() {
jQuery('.add').click(function() {
var wrapper = "<tr class='wrapper-tr'>" +
"<td class='value'><input type='file' name='images[]'></td>" +
"<td class='label'><span class='remove'>Remove</span></td>" +
"</tr>";
jQuery(wrapper).find('.remove').on('click', function() {
jQuery(this).parent('.wrapper-tr').remove();
});
jQuery(wrapper).appendTo('.container');
});
jQuery('.container').on('click', 'span.remove', function() {
if (jQuery('.wrapper-tr').length > 1) {
jQuery(this).parents('.wrapper-tr').remove();
} else {
alert('at least one image need to be selected');
}
});
});
</script>
用於上傳多個文件。
但正如我輸入類型名稱爲images[]
,這就是爲什麼在我的控制器的saveAction()
我無法用Varien_File_Uploader
爲上傳文件:
$uploader = new Varien_File_Uploader('images');
我應該在Varien_File_Uploader
構造函數傳遞以什麼樣的價值是能夠上傳文件?
更新
我試圖記錄和發現這樣的警告:
警告:file_exists()預計參數1是一個有效的路徑,在/ var/WWW/mageqb/LIB /瓦瑞恩數組給定/File/Uploader.php上線150
在我的控制器的代碼是:
foreach ($_FILES['images']['name'] as $key => $image) {
Mage::log('looping');
if (empty($image)) {
Mage::log('continue');
continue;
}
try {
Mage::log('uploading');
/* Starting upload */
$uploader = new Varien_File_Uploader('images');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
$uploader->setAllowRenameFiles(true);
// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS . 'authors' . DS;
$img = $uploader->save($path, $_FILES['images']['name'][$key]);
Mage::log($img['file']);
} catch (Exception $e) {
echo $e->getMessage();
Mage::log($e->getMessage());
}
}
感謝它幫了很大忙。 – ravisoni
Varien_File_Uploader有什麼用處 – Sri
@Sri:你怎麼看? –