php - 使用 FormData 和 PHP 上传多个文件
问题描述
我遇到了一个问题,我可以从一个表单中选择多个文件,但是当我在 PHP 中检查它时,它只显示一个文件(但在我的浏览器的控制台中,它显示两个文件)并且它没有移动文件到新位置。我不确定发生了什么。
这是我的代码:
jQuery:
$('#add-items-submit').on('click', function() {
var store_name = $('#main-store-name').text();
var item_name = $('#add-item-name').val();
var item_price = $('#add-item-price').val();
var item_desc = $('#add-item-desc').val();
var item_num = $('#add-item-number').val();
var formData = new FormData();
formData.append('store_name', store_name);
formData.append('item_name', item_name);
formData.append('item_price', item_price);
formData.append('item_desc', item_desc);
formData.append('item_num', item_num);
if (document.getElementById('add-item-image') == null) {
document.getElementById('add-items-message').value = "Please add an image of the item if you wish to submit it";
}
$.each($('#add-item-image')[0].files, function (i, file) {
formData.append('item_file', file);
});
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: '/user/add-items',
dataType: "json",
data: formData
}).done(function (msg) {
console.log(msg);
$('#add-items-message').html(msg.success);
}).fail(function (msg) {
console.log(msg);
$('#add-items-message').html(msg.failure);
});
});
PHP代码:
public function additemsAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
if ($this->getRequest()->isPost()) {
try {
$store_name = $this->params()->fromPost('store_name');
$item_name = $this->params()->fromPost('item_name');
$item_price = $this->params()->fromPost('item_price');
$item_desc = $this->params()->fromPost('item_desc');
$item_num = $this->params()->fromPost('item_num');
$item_image = $this->params()->fromFiles('item_file');
var_dump($item_image);
if ($this->getUserService()->addItemsToStore($store_name, $item_name, $item_desc, $item_image, $item_num, $item_price)) {
echo json_encode(array('success' => 'Item was successfully added to your store'));
}
} catch (\Exception $e) {
echo json_encode(array('failure' => $e->getMessage()));
}
}
return $view_model;
}
if (!is_dir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items')) {
mkdir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items', 0777, true);
}
for ($i=0; $i<count($item_image); $i++) {
if (is_uploaded_file($item_image['tmp_name'][$i])) {
move_uploaded_file($item_image['tmp_name'][$i], realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items/' . $item_image['name'][$i]);
$this->imagick = new \Imagick(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items/' . $item_image['name'][$i]);
// automatically enhance them
$this->imagick->enhanceImage();
$this->imagick->writeImagesFile(fopen(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items/' . $item_image['name'][$i], 'wb'));
}
}
所以,基本上它没有将文件移动到新位置并因此在它们上执行 imagick 的功能,但是我检查了我的日志并且没有在 apache 日志中写入错误,所以我真的很困惑。哦,我确实使用var_dump
过,结果是"array(5) {↵ ["name"]=>↵ string(40) "80E0C7D8-CEB1-49E5-839F-A08F0DE798CB.jpg"↵ ["type"]=>↵ string(10) "image/jpeg"↵ ["tmp_name"]=>↵ string(24) "C:\xampp\tmp\php89B6.tmp"↵ ["error"]=>↵ int(0)↵ ["size"]=>↵ int(68903)↵}
所以 formData 正在选择所有文件,但 PHP 只显示一个,而不是多个。
任何帮助,将不胜感激。
谢谢。
解决方案
推荐阅读
- node.js - 将标题元素添加到 paperjs SVG(使用 node.js)
- python - 从应用引擎访问谷歌云存储帐户对象
- ios - 应用程序通过 MDM 分发时的 Key Chain 数据丢失
- javascript - 如何将 angular2+ 应用程序作为简单的 JS 文件集成到现有的 HTML 站点中
- graph - 确定是否在正在运行的计划任务中的方法?
- python - 获取 AttributeError:“NoneType”对象没有属性“键盘”,
- d3.js - 如何获取 D3 Axis 的路径字符串
- java - Spring 数据 Neo4j + Kotlin 上的日期转换器
- jsf - 如何使用项目模板创建基于集合的组合?
- php - 动态 authTimeout:不覆盖 Yii2 中先前的 authTimeout 值