I don't get the download to work. I googled a lot but I don't see my fault, anybody does???

So what I did:

A csv file is created and stored in: $uri = "public://tmp".'/'.$filename;
For the moment this is: /sites/default/files/tmp/.
The file exists in the db.file_managed table and also a web-accessible URL for a stream to an external or local file.:

$file_object = file_save_data($data, $uri, FILE_EXISTS_RENAME);
$url = file_create_url($file_object->uri);

The file exists, it's all on my localhost so the permissions are:
(duke is my name on my pc, every folder has chmod -R 775)

- drwxrwxr-x 8 duke www-data  4096 Sep 10 19:59 files
- drwxrwxr-x 4 duke www-data 4096 Sep 10 20:42 tmp

A generated file has the following, e.g.:

-rw-rw-r-- 1 www-data www-data    5 Sep  9 22:27 adress_list_2015_09_09_22_27_11.csv

So I think that the Apache could read/write the file!???

I made a download link like this,

$options_download['attributes']['download'] = $filename;
   // We need query-params for download-token here.
   $dl_link = l(t('Download'), $url, $options_download);

... and set it in drupal_set_message():

  drupal_set_message(
     t('!download',array('!download'=>$dl_link))
    );

If I click on the link, I get the following text (translated from German):
File not found: Files under .../sites/default/files/tmp/adress_list_2015_09_10_21_10_10.csv could not be found.
If I refresh the site in browser I get:
You don't have permission to access /musik/sites/default/files/tmp/adress_list_2015_09_10_21_10_10.csv on this server. And I'm admin here!???

So does anybody knows what I'm doing wrong? As i stated: this is public file...

Thx in advance,

maen

Comments

nevets’s picture

I at least need to see the chunks of code in context. Note placing files in tmp is not a good practice. And from what you are showing you seem to be missing some code for managing the file.

maen’s picture

Without the creation of the content of data(Firstname etc. ...) this is my complete code:

    $now = date("Y_m_d_H_i_s");
    $filename='adress_list_'.$now.'.csv';
    $uri = "public://tmp".'/'.$filename;
    
    $file_object = file_save_data($data, $uri, FILE_EXISTS_RENAME);
    if (!empty($file_object)) {
      $url = file_create_url($file_object->uri);
      $_SESSION['adressliste_csv'] = $file_object->uri;
      $options_download['attributes']['download'] = $filename;
          $dl_link = l(t('Download'), $url, $options_download);
      drupal_set_message(
       t('!download',array('!download'=>$dl_link))
    );

The issue with tmp I will solve later. So which code do I miss????

Jaypan’s picture

The tmp file will not allow downloads for security reasons. Try saving to a different directory