我有问题,我有查看文件,但如何将这些上传保存到webroot /文件中。我正在使用CakePHP:
这是我的uploadfile.ctp
echo $this->Form->create('YourModel', array('type' => 'file','enctype'=>'multipart/form-data'));
echo $this->Form->input('files.', array('type' => 'file', 'multiple'));
echo $this->Form->end('Submit');
我不知道从哪里开始在Controller中,我真的需要这些文件到webroot / files,谢谢你!
目前我在Controller中:
public function uploadFile() {
if ($this->request->is('UploadFile')) {
$tmp_name=$this->request->data['UploadFile']['image'];
$filename = time().$this->request->data['UploadFile']['image']['name'];
if (move_uploaded_file($tmp_name['tmp_name'],WWW_ROOT."/files".$filename)) {
} else {
$this->Session->setFlash('There was a problem uploading file. Please try again.','default',array('class'=>'alert alert-danger'));
}
}
}
更新
现在我已经更新了视图文件和更新的Controller,我想上传多个文件,但只有一个文件进入文件夹。
查看文件:
<?php
echo $this->Form->create('uploadFile', array( 'type' => 'file'));
?>
<div class="input_fields_wrap">
<label for="uploadFilefiles"></label>
<input type="file" name="data[files]" 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');
控制器文件:
public function uploadFile() {
$filename = '';
if ($this->request->is('post')) { // checks for the post values
$uploadData = $this->data['files'];
print_r($this->data['files']); die;
if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) { // checks for the errors and size of the uploaded file
echo "Failide maht kokku ei tohi olla üle 5MB";
return false;
}
$filename = basename($uploadData['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($uploadData['tmp_name'], $uploadPath)) {
return false;
}
echo "Sa sisestasid faili(d): $filename";
}
}
和这个Javascript:
$(document).ready(function() {
var max_fields = 3;
var wrapper = $(".input_fields_wrap");
var add_button = $(".add_field_button");
var x = 1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
$(wrapper).append("<div><input type='file' name='data[files]' id='uploadFilefiles'/><a href='#' class='remove_field'>Kustuta</a></div>");
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
如何将所有3个文件上传到webroot/files
文件夹?
答案 0 :(得分:1)
try this code , this is a demo code and its is work on my server
<div class="col-sm-12">
<?php echo $this->Form->file('Feature.image.',array('class'=>'form-control','label'=>false,'div'=>false,'required','multiple'));?>
</div>
if($ this-&gt; request-&gt; is('post')){
$data=$this->request->data['Feature']['image'];
foreach ($data as $key => $value) {
$this->request->data['Feature']['image'][$key]['name'];
$tmp_name=$this->request->data['Feature']['image'][$key];
$filename = time().$this->request->data['Feature']['image'][$key]['name'];
if (move_uploaded_file($tmp_name['tmp_name'],WWW_ROOT."/img/feature/".$filename)) {
$updatefile= $this->Feature->updateAll(
array('Feature.image' => "'$filename'"),
array('Feature.id' => $id,'Feature.userid'=>$this->Session->read('Auth.User.id'))
);
if($updatefile==1){
$file = new File(WWW_ROOT . 'img/feature/'.$featuredata['Feature']['image'], false, 0777);
if($file->delete()) {
$this->Session->setFlash('File uploaded successfuly uploaded.','default',array('class'=>'alert alert-success'),'success');
return $this->redirect(array('controller'=>'Users','action'=>'featureshow')) ;
}
}
} else {
$this->Session->setFlash('There was a problem uploading file. Please try again.','default',array('class'=>'alert alert-danger'));
}
}
}
答案 1 :(得分:0)
不,当然你不需要桌子。 我想你在控制器中寻找这样的东西:
foreach($this->request->data['files'] as $file){
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'uploads/' . $uuid . '.jpg');
}
答案 2 :(得分:0)
使用: -
$uploadedFile = $this->request->params['form']['uploadCsv']['tmp_name'];
$dir = WWW_ROOT . 'files/';
if ( !is_dir( $dir ) ) {
mkdir($dir);
chmod( $dir , 777);
}
$fileName = 'file_' . date( 'Y_m_d_h_i_s', time() );
move_uploaded_file( $uploadedFile, $dir. $fileName . '.csv' );
这是一个可在我的服务器上运行的示例代码,也适用于您