1

I have a download-page where visitors can enter the name of a ZIP-file (without the extension) in an INPUT-field.

index.php :

<form action="download-script.php" method="post">
    <input type="text" name="file" placeholder="Enter filename here" />
    <input type="submit" value="Download starten" />
</form>

The ZIP-files are stored in a separate folder "files". If the visitor knows the name of the file everything is fine. If the file name is being misspelled or empty, an error message is displayed by the script:

download-script.php :

<?php

$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';

if (file_exists($file)) {
    header('Content-Disposition: attachement; filename=' . basename($file));
    header('Content-Type: application/force-download');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($file));
    header('Connection: close');
    readfile($file);
} 
else {
    echo "File not found";
    exit;
}
?>

My aim is to show the error-message on index.php and not on download-script.php because download-script.php will only show the error-message.

Shrikant Mavlankar
  • 1,145
  • 1
  • 8
  • 17
Hot Dog
  • 35
  • 8

2 Answers2

1

You can do something like this, let me know if it helps you.

index.php

<?php

if (isset($_POST['submit_form'])) {

    $file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
    $file_path =  'download/'. $file . '.zip';

    if (file_exists($file_path)) {

        echo "<script>window.location.href='download-script.php?file=".$file."'</script>";
    }
    else { echo "File not found"; exit; }
} 
?>

<form action="" method="post">
    <input type="text" name="file" placeholder="Enter filename here" />
    <input type="submit" name="submit_form" value="Download starten" />
</form>

download-script.php

<?php

$file = 'download/'. $_GET['file'] . '.zip';

if (file_exists($file)) {
    header('Content-Disposition: attachement; filename=' . basename($file));
    header('Content-Type: application/force-download');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($file));
    header('Connection: close');
    readfile($file);
}

Edit

Added $file_path variable for checking file exist or not in index.php and $file will be used for storing file name only which will be send to download-script.php

Please check now and let me know :)


Edit #2

I've better suggestion for you, When we can do all the stuff in single file then why we need another file for just download. I've added all the code in single index.php file, so no need to add javascript redirect also, which is I think causing the issue.

Please look in to below, and let me know if it helps you..

index.php

<?php

if (isset($_POST['submit_form'])) {

    $file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
    $file_path =  'download/'. $file . '.zip';

    if (file_exists($file_path)) {

        header('Content-Disposition: attachement; filename=' . basename($file_path));
        header('Content-Type: application/force-download');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Content-Length: ' . filesize($file));
        header('Connection: close');
        readfile($file);
    }
    else { echo "File not found"; exit; }
} 
?>

<form action="" method="post">
    <input type="text" name="file" placeholder="Enter filename here" />
    <input type="submit" name="submit_form" value="Download starten" />
</form>
Shrikant Mavlankar
  • 1,145
  • 1
  • 8
  • 17
  • Hi Shrikant Mavlankar! Thanks for your support! The error message works perfect! But the download-script doesn't work anymore. Something is wrong with the JavaScript ``. Usually the script works in the background. And now the javascript will load/show the **download-script.php** instead of loading the file. This link appears in the browser: **…/download-script.php?file=download/testfile.zip** and no file will be loading... – Hot Dog Jul 17 '16 at 22:48
  • Made few changes in my answer please check and let me know :) – Shrikant Mavlankar Jul 18 '16 at 02:14
  • Hi Shrikant! I have still the same problem: `` will load/show the script (a blank page), but not loading the file :-( – Hot Dog Jul 18 '16 at 06:16
  • Modified the code of `download-script.php`; Added missing `.zip` extension. Please check now, it should work. – Shrikant Mavlankar Jul 18 '16 at 06:29
  • Works perfect, thank you! But now we have a new problem: after pressing the download-button the page content starting after the form (form, footer) has disappeared. This is the generated source code: `[...] – Hot Dog Jul 18 '16 at 07:04
  • I've added better approach, please look and let me know. – Shrikant Mavlankar Jul 18 '16 at 08:15
  • Thank you again for your support. I have still the same problem with missing content after pressing the download-button. And now the file doesn't load :-( The solution by "dbugxpert" will work for me in the best way. Thank you for your help! ;-) – Hot Dog Jul 18 '16 at 08:26
1

In index.php

<?php
if (isset($_REQUEST["error"])) {
    echo "File not found";
}
?>
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>

In download-script.php

<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';

if (file_exists($file)) {
    header('Content-Disposition: attachement; filename=' . basename($file));
    header('Content-Type: application/force-download');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($file));
    header('Connection: close');
    readfile($file);
} 
else {
    header('Location: index.php?error=1');
    exit;
}
?>

You can try ajax but it's more complicated and harder to debug, see this or this question for an example.

Community
  • 1
  • 1
dbugxpert
  • 64
  • 3