error while trying to download file in codeigniter 3

Asked

Viewed 151 times

1

i made a function to upload files and this works properly, now in my view I added a link to download the file.

link in view:

<td><?php echo anchor("propostas/download/{$id}/{$record->nomepessoal}", "<i class='glyphicon glyphicon-arrow-down'></i>", ['class' => 'btn btn-primary btn-block', 'name' => 'baixar']); ?></td>

and this is the function of controller

// baixar anexo
public function download($id, $nomepessoal) {

    $this->load->helper('download');

    $nomearquivo = $nomepessoal."_".$id.".zip";
    $path = file_get_contents(base_url()."anexos/".$nomearquivo);

    force_download($path, null); // start download`
}

Error in function file_get_contents:

Message: file_get_contents(http://localhost/system/attachments/arquivo_17.zip): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

Note: the file exists and the name is correct.

1 answer

2

Validate if your . htaccess is configured and working. It should direct calls to index.php, but if accessing an existing file it should not direct to the index. Ensure that the lines below exist in your file .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
  • I set a rule Um sistema em cada VirtualHost and create an internal domain in my development environment, like http://sistema1.local/ and configure my archive /etc/hosts for my machine to recognize that domain, so I never wrote the Rewritebase line :)

  • @I edited it according to your guidance. I will pay attention to these details, although I was focused on making it work with what he presented and the url of the attachment is http://localhost/sistema/anexos/arquivo_17.zip) then I believe that Rewriteengine should point to the directory /sistema localhost is not right?

  • Let me put it another way: if sistema is the "root" of CI3, so this right, but is redundant, will work with or without, however even if it is redundant and does not affect anything, in the server/hosting the folder scheme may be totally different and so there yes can fail. There is no need to use Rewritebase unless you want the Rules to actually point to a folder that is totally different from the current one. + 1 for your answer, now I think you look great.

  • Perfect! I think I finally understand what Rewritebase is about and why it always worked without me, I never wanted it to point elsewhere! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.