I set the private file system to /system/files/export
, I create a file in private directory programmatically and it not belong to any field ,and show link to user , I implement Hook_file_download
, I want check some condition in it but when user click on link to download the file created in private path it return me Forbidden and mymodule_file_download
not tirggered ( I put some code to it , even die but never called).
I clear cache but noting changes,
the link that showed for user is http://localhost/mytestdrupal/system/files/export/Export-couponcards-1393307265.csv
where is problem?
Part of my code to create file in submit
function mymodule_export_form_submit($form, $form_state) {
$temp1 = variable_get('file_private_path');
if (!file_prepare_directory($temp1, FILE_CREATE_DIRECTORY)) {
drupal_set_message(t('Problem in write file in directory'), 'error');
return;
}
$filename = 'Export-file'. '.csv';
$filepath = $temp1 . '/' . $filename;
$df = fopen($filepath, 'w');
if (!$df) {
drupal_set_message(t('Can not write file in') . $filepath, 'error');
return;
}
fputcsv($df, array('code', 'name'));
fputcsv($df, array('test','test'));
fclose($df);
$path = file_create_url($filepath);
drupal_set_message(t('Export done. ') . '<a href="' . $path . '">' . t('Download export file') . '</a>');
}
http://localhost/mytestdrupal/system/files/export/Export-couponcards-1393307265.csv
variable_get('file_public_path');
not the private file path. Note also the menu callback usessystem/files
but your private folder path should probably be different, e.gsites/default/files/private
or somewhere outside the webroot.