This article describes the example of yii implementing the method of using CUploadedFile to upload files. 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. Backend 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:
Copy the code via The code is as follows: CUploadedFile::getInstance($model,'album_image');
Or Copy the code The code is as follows: $attach = CUploadedFile::getInstanceByName($inputFileName);
The obtained object $attach object has the following attributes:
name
size
type
tempName
error
extensionName
hasError
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.