简体   繁体   中英

file download in yii shows file not exist

I am trying to write a script in Yii for downloading files.
view file is

echo CHtml::link('Major Oilseeds: World Supply and Distribution.','downloadpdf', array('class'=>'btn btn-darkorange'));

and the controller code is

$path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv'; echo $path; if(file_exists($path)){ Yii::app()->getRequest()->sendFile( 'Major_Oilseeds.csv' , file_get_contents($path) ); } else{ echo '<br>File not found'; }
the code echo $path dispalys the location as
http://localhost/projectv2/download/Major_Oilseeds.csv
and the download folder contains the file named "Major_Oilseeds.csv" but its always showing "File Not Found" Error. plz somebody help me to solve this. I have also tried the code

$path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv'; // $filename = 'Major_Oilseeds.csv'; header('Content-Disposition: attachment; charset=UTF-8; filename="'.$path.'"'); $utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8"); echo $utf8_content; Yii::app()->end(); return;

but its also not working :-(

file_exists can only work with local files. In your case you are trying to use file_exists with a URL.

You can find a workaround here: http://uk1.php.net/file_exists (please search for "http" in this page)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM