I'm putting a page on my website to allow downloading of files with formats such as pdf, jpg, etc. I use a link in the html page: <a href="download.php?file=picture.jpg"> ... </a> the file "download.php" looks like this: <?php echo "Hello"; $file = $_GET['file']; header (“Content-type: octet/streamâ€); header (“Content-disposition: attachment; filename=â€.$file.“;â€); header(“Content-Length: â€.filesize($file)); readfile($file); exit; ?> The link doesn't work, however. The link opens a blank page with the correct address in the address bar. There is no "page not found" message, so the php file is correctly uploaded to my website, but no file download prompt appears. I inserted the second line (echo "Hello") to see if any of the php script worked, and it doesn't. There's no "hello" message, and no file download. Am I missing something from the php page? I tried enclosing it in <html> and <body> tags, but it still didn't work. Any advice would be appreciated.
Try removing Also, DO NOT USE THIS SCRIPT!!! it is a MAJOR security hole. Anyone could hack your entire server with this.
<?php ob_start(); echo "Hello"; $file = $_GET['file']; header ("Content-type: octet/stream"); header ("Content-disposition: attachment; filename=".$file.";"); header("Content-Length: ".filesize($file)); readfile($file); exit; ?> PHP: There should not be any output (even whitespace) before header() function. In the script u output a hello so it doesn't work. Use ob_start() just after <?php can overcome that. try using it?
This is straight out of the last script I worked on with the only change being I didn't include a type-setting function I have in use which is irrelevant to this code. HTH $downloading = $_GET['downloading']; if(preg_match('#^[0-9a-f]{32}$#i', $downloading)) { $wallpaper_file = "$wallpapers_path/$downloading.jpg"; header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition: attachment; filename="iPhone-Wallpaper.jpg";'); header('Content-Length: ' . filesize($wallpaper_file)); flush();// Get the headers out there early so the browser can get to work @set_time_limit(0); @readfile($wallpaper_file); flush(); exit; } unset($downloading); Code (markup):
thanks everyone for your help, I'm going to do more research on php language and security issues before I try using php.