我有一个代码,其中包含上传到文件夹的文件数量,我希望这些文件的名称,大小和URL在数据库中,但我的Controller无法正常工作。 (我使用CakePHP框架)。我想添加这些文件数据我上传到数据库(所有文件数据),我收到错误。
错误:
Notice (8): Undefined index: tmp_namā€ā€‹ā€ā€‹e [APP\Controller\UploadFilesController.php, line 24]
我的控制器
public function uploadFile() {
$filename = '';
if ($this->request->is('post')) { // checks for the post values
$uploadData = $this->request->data;
//print_r($this->request->data); die;
foreach($uploadData as $file){
$filename = basename($file['name']); // gets the base name of the uploaded file
$uploadFolder = WWW_ROOT. 'files'; // path where the uploaded file has to be saved
$filename = $filename; // adding time stamp for the uploaded image for uniqueness
$uploadPath = $uploadFolder . DS . $filename;
if( !file_exists($uploadFolder) ){
mkdir($uploadFolder); // creates folder if not found
}
if (!move_uploaded_file($file['tmp_name'], $uploadPath)) {
return false;
}
echo "Sa sisestasid faili: $filename<br>";
}
foreach($this->request->data['UploadFile']['file_upload'] as $file){
if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['tmp_name'])) { //THIS IS LINE 24
$fileData = fread(fopen($this->request->data['UploadFile']['file_upload']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['size']);
$this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['name'];
$this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['size'];
$this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['tmp_name'];
$this->request->data['UploadFile']['data'] = $fileData;
$this->UploadFile->create();
$this->UploadFile->save($this->request->data);
}
}
}
}
}
这是我的查看文件:
<?php
echo $this->Form->create('uploadFile', array( 'type' => 'file'));
?>
<div class="input_fields_wrap">
<label for="uploadFilefiles"></label>
<input type="file" name="data[]" id="uploadFilefiles">
</div>
<button type="button" class="add_field_button">+</button> <br><br>
<form name="frm1" method="post" onsubmit="return greeting()">
<input type="submit" value="Submit">
</form>
<?php
echo $this->Html->script('addFile');
如果有必要,我还可以添加AddFile
脚本。
这是我的表格( upload_files )结构:
答案 0 :(得分:5)
我认为,这是最好的方法:
public function uploadFile() {
$filename = '';
if ($this->request->is('post')) { // checks for the post values
$uploadData = $this->request->data ['UploadFile']['file_upload'];
//print_r($this->request->data); die;
foreach($uploadData as $file){
$filename = basename($file['name']); // gets the base name of the uploaded file
$uploadFolder = WWW_ROOT. 'files'; // path where the uploaded file has to be saved
$filename = $filename; // adding time stamp for the uploaded image for uniqueness
$uploadPath = $uploadFolder . DS . $filename;
if( !file_exists($uploadFolder) ){
mkdir($uploadFolder); // creates folder if not found
}
if (!move_uploaded_file($file['tmp_name'], $uploadPath)) {
return false;
}
echo "Sa sisestasid faili: $filename<br>";
$this->request->data['UploadFile']['name'] = $file['name'];
$this->request->data['UploadFile']['size'] = $file['size'];
$this->request->data['UploadFile']['URL'] = $file['tmp_name'];
$this->UploadFile->create();
$this->UploadFile->save($this->request->data);
}
}
}
我在我的主人和它的工作中尝试了这个。
答案 1 :(得分:1)
您在视图中更改:
<input type="file" name="data[UploadFile][file_upload][]" class="form-control" multiple="multiple" id="AuthorAuthorImage">
你得到控制器
$this->request->data['UploadFile']['file_upload']
此数据是格式为:
的数组array (
[0] => Array
(
[name] => 0.jpg
[type] => image/jpeg
[tmp_name] => D:\xampp\tmp\php5896.tmp
[error] => 0
[size] => 55125
)
[1] => Array
(
[name] => 1.jpg
[type] => image/jpeg
[tmp_name] => D:\xampp\tmp\php5897.tmp
[error] => 0
[size] => 49613
)
[2] => Array
(
[name] => 1-16.png
[type] => image/png
[tmp_name] => D:\xampp\tmp\php58A7.tmp
[error] => 0
[size] => 1545337
)
)
祝你好运!
答案 2 :(得分:1)
正如tungbk29所说,$ this-&gt; request-&gt; data [&#39; UploadFile&#39;] [&#39; file_upload&#39;]是数组,因此您应该像这样更改foreach代码
if (!empty($this->request->data['UploadFile']['file_upload'])) {
foreach($this->request->data['UploadFile']['file_upload'] as $file){
if (is_uploaded_file($file['tmp_name'])) {
$fileData = fread(fopen($file['tmp_name'], "r"), $file['size']);
$this->request->data['UploadFile']['name'] = $file['name'];
$this->request->data['UploadFile']['size'] = $file['size'];
$this->request->data['UploadFile']['URL'] = $file['tmp_name'];
$this->request->data['UploadFile']['data'] = $fileData;
$this->UploadFile->create();
$this->UploadFile->save($this->request->data);
}
}
}
希望它再次有用!