Jump to content

Problem feeding files to users


Stooney

Recommended Posts

The code below works for .txt files, but just displays a ton of jibberish on the screen when used with .jpg.  With .txt the users will get the option to download/open the file as they should, just not with .jpg (haven't tested other file types)

 

<?php
function request_file($cat, $file){
$fileid=mysql_real_escape_string($file);
$get=mysql_query("SELECT fname, path FROM dtf_files WHERE id='$fileid'");
$row=mysql_fetch_array($get);
if($row){
	$basefilename=$row[0];
	$filename=$row[1].$row[0];
		echo $filename;
	if(false !== ($fh = fopen($filename, 'r'))){
		header("Cache-Control: no-cache, must-revalidate"); 
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=$basefilename");
		while(!feof($fh)) {
			echo fread($fh, 1024);
		}
	}
	else{
		$error='SERVER ERROR:  Could Not Open File!';
		echo '<div class="error">'.$error.'</div>'."\n";
		show_files($cat);
	}
}
else{
	$error='SERVER ERROR:  Could not retrieve file list from database!';
	echo '<div class="error">'.$error.'</div>'."\n";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/83024-problem-feeding-files-to-users/
Share on other sites

This is the code I'm working off of, (originally posted by Corbin I think).  It works fine for all file types:

 

 

if(false !== ($fh = fopen($filename, 'r'))) {

	//These headers would make the page not be cached by the web browser
	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

	//this would tell the browser to download it instead of showing it
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$sname"); //suggest a filename
	while(!feof($fh)) {
		echo fread($fh, 1024); //read and echo [up to] 1024 bytes at a time
	}
}

HEADERS

 

<?PHP

// build file headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

// header for the content type
$ext = strToLower(substr($filename,strlen($filename)-3, 3));
if ($ext == "mp3" ) { header("Content-Type: audio/x-mp3"); }
else if ($ext == "jpg") { header("Content-Type: image/jpeg"); }
else if ($ext == "gif") { header("Content-Type: image/gif"); }
else if ($ext == "png") { header("Content-Type: image/png"); }
else if ($ext == "swf") { header("Content-Type: application/x-shockwave-flash"); }
else if ($ext == "flv") { header("Content-Type: video/flv"); }

// and some more headers
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

// refer to file and exit
readfile("$filename");
exit();
?>

Alright, I changed the headers to what you suggested redarrow and it works fine in its own script.  But when I turn it into a function, and call it from somewhere in the site, gibberish is still displayed on the page.  I'm assuming it's because you cant change the headers when there's already been stuff written to the page.  Is there a way around this?  Or should I just send the users to download.php rather than call it as a function?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.