Home >Backend Development >PHP Tutorial >Yii implements the method of uploading files using CUploadedFile
The example in this article describes how yii implements uploading files using CUploadedFile. Share it with everyone for your reference, the details are as follows:
1. Front-end code
Html code:
<form action="<?php echo $this->createUrl('/upload/default/upload/');?>" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="hidden" name="dir" value="<?php echo Yii::app()->controller->currentDir?>"/> <input type="submit" value="Upload Image"/> </form>
2. Back-end code
Php code:
public function actionUpload() { $this->currentDir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : ''; $image = CUploadedFile::getInstanceByName('file'); $name = $this->uploadPath.'/'.$this->currentDir.'/'.$image->name; $image->saveAs($name); $this->redirect(array('index','dir'=>$this->currentDir)); }
About the use of CUploadedFile class:
Passed
Copy the code The code is as follows:
CUploadedFile::getInstance($model,'album_image');
Copy the codeThe code is as follows:
$attach = CUploadedFile::getInstanceByName($input FileName );
The obtained object $attach object has the following attributes:
name
size
type
tempName
error
extensionName
hasError
I hope this article will be useful to everyone based on the Yii framework PHP programming helps.
The above introduces Yii's method of using CUploadedFile to upload files, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.