file download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

file download

Post by ale2121 »

Hi again,
i'm trying to download files, but I keep getting an error that the file doesn't exist. But it does. I have the permissions to my uploads folder set to 777, even though I'm not supposed to, and if i remove the if file_exists statement i get an error saying :
Warning: readfile(http://www.subversivemash.com/uploads/302366.m4a): failed to open stream: No such file or directory in /var/www/html/download_file.php on line 10
also if i try to just type the url to the file in my uploads folder, it doesn't work, i get a page not found error.
the $_GET['upname'] is sent from another page.

Code: Select all

<?php
if (isset($_GET['upname'])) {
	$the_file='www.subversivemash.com/uploads/' . $_GET['upname'];
	if (file_exists($the_file)) {
		readfile($the_file);
		$message = '<p>The file has been sent</p>';
	} else {
		$message = '<p><font color = "red"> could not locate file.</font></p>';
	}
} else {
	$message = '<p><font color = "red">Please select a valid file to download.</font></p>';
}
echo $message;
?>

feyd | I'm getting tired of changing your ending

Code: Select all

tag to a
:?[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file_exists() for urls only works in PHP 5, fyi.

Your url doesn't have a http:// on it, so php doesn't know it's a url, it thinks it's a local file.



.. and please.. start using the ending tag instead of
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

$contents = @readfile($the_file); 
if ($contents) {
  echo 'Success!';
}
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

what could i use instead of readfile() ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oops, my bad. I forgot that readfile() directly outputted file contents.

file_get_contents() works great. http://us2.php.net/manual/en/function.f ... ntents.php
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

if i can't get the file to download when i type the url into my browser, then there's probably no way php can get it either right? is this something with my server? i have the permissions to the uploads folder set to 777.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php has far more system access often than a url can get you. What's the purpose of this script?
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

i'm trying to download files that are stored in an upload folder.
i'm using php4.3, so readfile() doesn't work. I don't really understand the get_file_contents() that ambush commander sent, so i don't know how to use it in my script. i also tried to use the header() function, but that was a no go too.
sorry to keep asking such stupid questions.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are these recently uploaded files, as in, this script is an uploaded file processor? Or is it simply a file downloader?

At any rate, you don't use URL's to get access to it from php, instead use the local file system paths so php can open the file and transmit as needed.

Do you know the system path to the upload folder? If you don't, do you know where it is in relation to the webroot?
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

right now, the upload folder is in my root directory (the same as the script), until I can get this working.
the scripts are uploaded from a different script.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if they are in the current directory, then you don't need anything but the filename. :)
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

i've tried lots of variations on the file name/path, including local and not local. this code returns a failed to open stream, no such file or directory error.

Code: Select all

<?php
	readfile("/uploads/299681.pdf");
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried removing that first slash?
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

ok, so I got to the file, I realized it was downloading, but pumping out all this crazy data, like it's not reading the file correctly. so i added these headers to tell it how to handle the file.
but this is causing another error. it says Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/download_file.php on line 6 I'm also getting an error because the headers say info was already sent to the url, makes sense for the 2nd and 3rd, but not the first.

Code: Select all

require_once ('mysql_connect.php');
	$query = "SELECT file_name, file_type, file_size FROM soundfiles WHERE upname = {$_GET['upname']}";
	$result = mysql_query($query);
	list($fn, $ft, $fs) = mysql_fetch_array($result);
	mysql_close();
	$extension = explode('.',$fn);
	$the_file='uploads/' . $_GET['upname'].'.'. $extension[1];
	//if (file_exists($the_file)) {
		header("Content-Type: application/$ft");
		header("Content-disposition: attachment; filename=$fn");
		header("Content-Length: $fs");
		readfile($the_file);
		$message = '<p>The file has been sent</p>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your query probably has a syntax error. check what mysql_error() tells you.
Post Reply