上传文件中的数据 [英] Data from uploaded file
问题描述
我有一个代码,可以是多少文件上传到文件夹,我想要这些文件名称,大小和URL到数据库,但我的控制器不工作。 (Im使用CakePHP框架)。我想将这些文件数据添加到数据库中(所有文件数据),并得到错误。
I have a code with what can be the number of files uploaded in to folder and I want these files name, size and URL in to database, but my Controller is not working. (Im using CakePHP framework). I want to add these files data what Im uploading in to database (all files data) and I got error.
错误:
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);
}
}
}
}
}
$ b b
这里是我的查看文件:
<?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
If it is necessary I can also add AddFile
script.
这是我的表格( upload_files )结构:
Here is my table (upload_files) structure:
img src =http://i.stack.imgur.com/p6Xbd.jpgalt =enter image description here>
推荐答案
我认为,这是最好的方法:
I think, this is the best way to do this :
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);
}
}
}
和它的工作。
这篇关于上传文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!