1

I want to provide download link to the users to download the files which are present in the public folder. How can I achieve that, I tried below but not worked.

if ($handle = opendir(variable_get('file_public_path', conf_path(). '/files/') .'/xmluploads/Sainath')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $file = basename($_GET[$entry]);//$file consists caste certificate.pdf
            $file = variable_get('file_public_path', conf_path(). '/files/') .'/xmluploads/Sainath'.$file; 
            if(!$file){ // file does not exist
                die('file not found');
            } else {
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header("Content-Disposition: attachment; filename=$file");
                header("Content-Type: application/zip");
                header("Content-Transfer-Encoding: binary");

                // read the file from disk
                readfile($file);
            }
        }
    }
    closedir($handle);
}

This error I am getting:

Notice: Undefined index: caste certi.pdf in file_upload_download() (line 194 of C:\xampp\htdocs\academy\sites\all\modules\custom\file_upload\file_upload.module).
Warning: readfile(sites/default/files/xmluploads/Sainath): failed to open stream: Permission denied in file_upload_download() (line 206 of C:\xampp\htdocs\academy\sites\all\modules\custom\file_upload\file_upload.module).

Please help regarding this, Thanks in advance..

1

I think just giving a link to the file itself will download the file. In case of an image or PDF file, won't opening the file in new tab from where they can download suffice for you?

Regards, Supriya Rajgopal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.