0

I'm downloading a file through php script and everything work perfectly except one ugly truth. The downloaded file keep the same url and with original name appended. How do I maintain the same filename when file downloaded?

http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx
if (isset($_GET['mfile'])) {
    $file = $_SERVER['DOCUMENT_ROOT'].'/oc-content/plugins/mmailer/pfile/'.$_GET['mfile'];

    if (file_exists($file) && is_readable($file) && preg_match('/\.docx$/',$file)) {
        header('Content-Type: application/docx');
    header("Content-Disposition: attachment; filename=\"$file\"");
    readfile($file);
    /*
    header("Expires: 0");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    echo (readfile($file));*/


    }
    else
    {
        header("HTTP/1.0 404 Not Found");
        echo "Error 404: File Not Found: 
$file"; }
1
  • 1
    try this code. header("Content-Disposition: attachment; filename={$name}.{$file_ending}"); the difference is in concatenating. Commented Aug 19, 2013 at 5:20

3 Answers 3

3

header('Content-Disposition: attachment; filename="name_of_file_here"') would have done the trick. You are passing the full path of the file there as header("Content-Disposition: attachment; filename=\"$file\""); since your $file contains full path. Instead just send the name of the file.

Sign up to request clarification or add additional context in comments.

Comments

1

header('Content-Disposition: attachment; filename='.basename($file));

This line can perhaps solve the issue.

2 Comments

Please explain how can this code possibly solve the issue.
.basename($file) would return the last part(the file name along with the extension) from the $file string, which is the issue asked above.
0
if (isset($_GET['mfile'])) {
    $file = $_SERVER['DOCUMENT_ROOT'].'/oc-content/plugins/mmailer/pfile/'.$_GET['mfile'];

    if (file_exists($file) && is_readable($file) && preg_match('/\.docx$/',$file)) {
        header('Content-Type: application/docx');
    header("Content-Disposition: attachment; filename=\"".basename($file)."\"");//use basename to extract filename from full file path
    readfile($file);
    /*
    header("Expires: 0");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    echo (readfile($file));*/


    }
    else
    {
        header("HTTP/1.0 404 Not Found");
        echo "Error 404: File Not Found: 
$file";
    }

2 Comments

"...filename=\"basename($file)\"" doesn't do what you think it does.
Sorry it's my mistake... forgot to keep the function out of quotations.. now you can use the original filename in the server and send it to the downloaded file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.