gv0zd
(Gv00000zd)
May 28, 2012, 8:09am
1
Hello
I want to upload file, read the content, output it, but i don’t want to save this file on server
public function actionUpload()
{
$model=new UploadForm;
if(isset($_POST['UploadForm']))
{
$model->attributes=$_POST['UploadForm'];
if($model->validate()) {
$model->file=CUploadedFile::getInstance($model,'file');
//how to output content of uploaded file?
}
}
}
gv0zd
(Gv00000zd)
May 28, 2012, 8:21am
2
Is it right way?
$file=CUploadedFile::getInstance($model,'file');
$content=file_get_contents($file->tempName);
emduck
(Em)
May 28, 2012, 9:02am
3
The server can’t get the file’s contents, as the file resides on the client. You could simply remove the file after uploading it and getting its contents:
$uploadedFile = CUploadedFile::getInstance($model, 'file');
$newFilePath = "tmp/{$uploadedFile->name}";
$uploadSuccess = $uploadedFile->saveAs($newFilePath);
if (!$uploadSuccess) {
throw new CHttpException('Error uploading file.');
}
$content=file_get_contents($newFilePath);
unlink($newFilePath);