brownka Posted May 3, 2007 Share Posted May 3, 2007 This has been driving me nuts. I have a database set up that stores a $filepath and other file info and a download script to retrieve the file and download it. Whenever I try to download a file it changes the name of the file im trying to download to the name of the php page (example: filename:"ads876dfa8798adfdf.txt" but it downloads as "fdownload.php". I beleive that i have all the content header set properly but i need some fresh eyes. Any help would be appreciated! fdownload.php ------------------------------------------------------------------------------------------------------ <?php if ($_GET['submitnum']) { include ("dbconnect.php"); $submitnum = $_GET['submitnum']; $query = "select filedata, filetype, filename, filesize from submitted where submitnum=$submitnum"; $result = @mysql_query($query, $conn); list($filename, $filetype, $filesize, $filepath) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$filename"); header("Content-length: $filesize"); header("Content-type: $filetype"); readfile($filepath); exit; } ?> ------------------------------------------------------------------------------------------ fupload.php ------------------------------------------------------------------------------------------ if($_POST['file'] != "none" && $_FILES['file']['size'] > 0) { $uploaddir = "C:/Apache2/htdocs/finalproject/posted/"; $classnum=$_POST['class']; $description=$_POST['description']; $duedate=$_POST['duedate']; $file=$_POST['file']; $filename = $_FILES['file']['name']; $tmpname = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; $filetype = $_FILES['file']['type']; $ext = substr(strrchr($filename, "."), 1); $randname = md5(rand() * time()); $filepath = $uploaddir . $randname . '.' . $ext; $result = move_uploaded_file($tmpname, $filepath); $query = "insert into posted (userid, duedate, classnum, description, filename, filesize, filetype, filepath ) values ('$id', '$duedate', '$classnum','$description', '$filename', '$filesize', '$filetype', '$filepath')"; $result=mysql_query($query) or die("query error"); if ($result==0) { echo "An error has occurred. Assignment was not posted."; } else { echo "You have successfully posted an assignment."; } } else { echo "You must select a file for upload!"; } } -------------------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
trq Posted May 3, 2007 Share Posted May 3, 2007 Try... header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=". basename($filename)); header("Content-Description: File Transfer"); Quote Link to comment Share on other sites More sharing options...
brownka Posted May 3, 2007 Author Share Posted May 3, 2007 Thanks for the suggestion but i found an error in my code....i highlighted it below. i was selecting the a blob from a previous try (saving the file in a blob in mysql) but i should have been selecting the filepath since im storing the file in a fodler on the server now. But it still does not work. It now downloads and saves my files with the full path in the name (ex file is on server as "b3e9e4e56d146bdb45ef727f30986f95.doc" but saves as "C--Apache2-htdocs-website-submitted-b3e9e4e56d146bdb45ef727f30986f95" when i download it.). And to top it off the file is corrupt and cant be read. Any more suggestions? I know this shouldnt be this hard. Thanks!! ------------------------------------------------------------------------------------------------------------------------------------------ fdownload.php ------------------------------------------------------------------------------------------------------ <?php if ($_GET['submitnum']) { include ("dbconnect.php"); $submitnum = $_GET['submitnum']; $query = "select filedata, filetype, filename, filesize from submitted where submitnum=$submitnum"; $result = @mysql_query($query, $conn); list($filename, $filetype, $filesize, $filepath) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$filename"); header("Content-length: $filesize"); header("Content-type: $filetype"); readfile($filepath); exit; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.