Page 1 of 2

file download

Posted: Sun Aug 28, 2005 12:01 pm
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]

Posted: Sun Aug 28, 2005 12:14 pm
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

Posted: Sun Aug 28, 2005 12:26 pm
by Ambush Commander

Code: Select all

$contents = @readfile($the_file); 
if ($contents) {
  echo 'Success!';
}

Posted: Sun Aug 28, 2005 12:54 pm
by ale2121
what could i use instead of readfile() ?

Posted: Sun Aug 28, 2005 12:56 pm
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

Posted: Sun Aug 28, 2005 1:28 pm
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.

Posted: Sun Aug 28, 2005 1:34 pm
by feyd
php has far more system access often than a url can get you. What's the purpose of this script?

Posted: Sun Aug 28, 2005 1:46 pm
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.

Posted: Sun Aug 28, 2005 1:56 pm
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?

Posted: Sun Aug 28, 2005 2:07 pm
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.

Posted: Sun Aug 28, 2005 2:42 pm
by feyd
if they are in the current directory, then you don't need anything but the filename. :)

Posted: Sun Aug 28, 2005 3:19 pm
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");
?>

Posted: Sun Aug 28, 2005 3:20 pm
by feyd
have you tried removing that first slash?

Posted: Sun Aug 28, 2005 11:11 pm
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>';

Posted: Sun Aug 28, 2005 11:21 pm
by feyd
your query probably has a syntax error. check what mysql_error() tells you.