PHP - simple script loading

I am doing a simple loading script where my users can upload their own images, etc.

But I have some strange problem.

When I uploaded the file, it had the contents from my index.php file no matter what type of file it uploaded. My code looks like this:

$fullPath = $r['snptFilepath'] . $r['snptFilename']; if (file_exists($fullPath)) { #echo $fullPath; // setting headers header('Content-Description: File Transfer'); header('Cache-Control: public'); # needed for IE header('Content-Type: '.$r['snptFiletype'].''); header('Content-Disposition: attachment; filename='. $filename . '.' . $r['snptExtension']); header('Content-Length: '.$r['snptSize'].''); readfile($fullPath)or die('error!'); } else { die('File does not exist'); } 

$ r is the result from my database, where I saved the size, type, path, etc., when the file is uploaded.

UPDATE

When I upload and upload * .pdf files, it works with success. But when I try to download * .zip and text / rtf, the text / plain it acts strange.

By strange, I mean: it loads the full index.php file with the loaded contents of the file inside it.

ANSWER

I copied this from http://php.net/manual/en/function.readfile.php and now works. It seems that: ob_clean (); did the trick! Thanks for helping everyone.

 #setting headers header('Content-Description: File Transfer'); header('Content-Type: '.$type); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; 
+4
source share
2 answers

I copied this from http://php.net/manual/en/function.readfile.php and now it works. ob_clean (); did the trick ..

  #setting headers header('Content-Description: File Transfer'); header('Cache-Control: public'); header('Content-Type: '.$type); header("Content-Transfer-Encoding: binary"); header('Content-Disposition: attachment; filename='. basename($file)); header('Content-Length: '.filesize($file)); ob_clean(); #THIS! flush(); readfile($file); 
+5
source

Try this feature or include these headers in your code

  function force_download($filename) { $filedata = @file_get_contents($filename); // SUCCESS if ($filedata) { // GET A NAME FOR THE FILE $basename = basename($filename); // THESE HEADERS ARE USED ON ALL BROWSERS header("Content-Type: application-x/force-download"); header("Content-Disposition: attachment; filename=$basename"); header("Content-length: " . (string)(strlen($filedata))); header("Expires: ".gmdate("D, d MYH:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT"); header("Last-Modified: ".gmdate("D, d MYH:i:s")." GMT"); // THIS HEADER MUST BE OMITTED FOR IE 6+ if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE ')) { header("Cache-Control: no-cache, must-revalidate"); } // THIS IS THE LAST HEADER header("Pragma: no-cache"); // FLUSH THE HEADERS TO THE BROWSER flush(); // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END ob_start(); echo $filedata; } // FAILURE else { die("ERROR: UNABLE TO OPEN $filename"); } } 
+6
source

Source: https://habr.com/ru/post/1432661/


All Articles