Home >Backend Development >PHP Tutorial >Yii implements the method of uploading files using CUploadedFile

Yii implements the method of uploading files using CUploadedFile

WBOY
WBOYOriginal
2016-07-29 09:08:401023browse

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="<&#63;php echo $this->createUrl('/upload/default/upload/');?>" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="hidden" name="dir" value="<&#63;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');


or

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn