Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP – How user can set directory where downloaded file will be saved?

I am making a simple application in which the user uploads files using 2 HTML input:file fields and a zip archive is created from them. Is there any way I can let the user choose where this zip can go? (Something like a "Save As" window).

This is my current solution, which saves archive only to default download destination without asking.

    $files = array($_FILES["fileone"]["name"], $_FILES["filetwo"]["name"]);
    $zipname = "myarchive.zip";
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile($zipname);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is not possible for a PHP script to prompt the user for a location to save a file. This kind of functionality is typically handled by the user’s web browser.

However, you can provide a download link for the file that the user can click on to download the file. When the user clicks on the link, their web browser will prompt them to choose a location to save the file.

Here is an example of how you could provide a download link for the zip file that your PHP script creates:

$files = array($_FILES["fileone"]["name"], $_FILES["filetwo"]["name"]);
$zipname = "myarchive.zip";
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
    $zip->addFile($file);
}
$zip->close();

// Provide a link for the user to download the zip file
echo '<a href="' . $zipname . '">Click here to download the zip file</a>';

When the user clicks on the link, their web browser will prompt them to choose a location to save the file.

Note: If you want to force the web browser to download the file instead of opening it, you can use the Content-Disposition HTTP header. Here is an example of how you could set this header to force the file to download:

header('Content-Disposition: attachment; filename="' . $zipname . '"');

This will tell the web browser to download the file and save it with the specified filename, instead of opening it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading