i’m using a force-download script on my own server. – click on a link and the file prompts as download. works perfect, but even if a file does not exist it can be downloaded as empty file. so if i set a link with /folder/filexyz.txt it prompts the download and an empty txt-file gets downloaded to the harddrive.
i’m currently using this script:
header("Cache-Control: no-cache");
header("Expires: -1");
header("Content-Type: application/octet-stream;");
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
echo readfile($file);
$file is the complete path to my file like: http://www.mydomain.com/folder1/folder2/folder/filexyz.txt
how can i check if the file exists or not. If it’s not there the download should not prompt, otherwise of course yes.
you need to store the LOCAL path to the file in
$pathnot a web-address!like
C:\path\to\somefile.txton windows or/path/to/somefile.txton linuxalso do not use
file_exists()butis_file()instead, because file_exists also returns true if the given path is a directory and not a file.