1

Greetings, I have a folder with many pdf files. What i want is to be able to generate links in a view, and when i click one of those links the browser render's the file. In last case, at least the browser downloads the file to the hard-drive. Neither of both could implement.

For now i have this code in my index.php file. It sets the pdfs folder and list the files in my browser window because of the foreach loop.

<?php
$cfileDir = Yii::app()->file->set('pdfs/');
$ficheiros = $cfileDir->getContents();
if (isset($ficheiros[0])) {
foreach ($ficheiros as $index => $ficheiro) {
    $nomeFicheiro = substr($ficheiro, strrpos($ficheiro, '/') + 1);
    echo CHtml::link($nomeFicheiro, array('pdfs/'.$nomeFicheiro)) . "<br/>";


   }
} else {
    echo "Não existem ficheiros disponíveis para download.";
}
?>

This code shows the links generated for each file inside the folder PDFS, but when i click on a link it display ERROR 404 could not resolve the requisition "pdfs/nameoffile.pdf". I don't have any code im my Controller, is it needeed ? I use the extension Cfile and its methods to manage files, but i don't think it as influence on the ERROR 404.

**

What to do... Any solutions ? Thanks in advance.

**

André Castro
  • 1,527
  • 6
  • 33
  • 60

1 Answers1

0

Change it to:

echo CHtml::link($nomeFicheiro, Yii::app()->baseUrl.'/pdfs/'.$nomeFicheiro) . "<br/>";

As array('pdfs/'.$nomeFicheiro) is the link to pdfs controller and $nomeFicherio action which it seems you don't have it.

On the other hand, it is suggested not to break MVC structure and separation of concerns.

Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
  • 1
    SOLVED. Your suggestion solved the issue, and now everything works the way i wanted to. Also, many thanks for the valuable explanation about how things work. Thank you very much. – André Castro Nov 16 '14 at 17:53