tags:

views:

29

answers:

3

hey guys, 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.

+3  A: 

You can use file_exists() to check if the file exists :) If it doesn't send a 404 error

header("HTTP/1.0 404 Not Found"); 
Goran Jurić
better use is_file() because file_exists() also is true for directories!
zolex
and while we're at it: `is_readable()` :)
Wrikken
A: 

Did you tried with file_exists1?

bool file_exists ( string $filename )
JuanIgnacioIglesias
A: 

you need to store the LOCAL path to the file in $path not a web-address!

like C:\path\to\somefile.txt on windows or /path/to/somefile.txt on linux

also do not use file_exists() but is_file() instead, because file_exists also returns true if the given path is a directory and not a file.

zolex
lol why is this -1 ^^
zolex