brownka Posted May 4, 2007 Share Posted May 4, 2007 I am storing the path to an uploaded file in a mysql database and i want to download that file and save it. I keep getting corrupt, garbled, and misnamed files when i download them. I have checked the uploaded file on my server and so i know that it is intact. Any help would be great or if anyone has a script that already works i would love to look at it! This is driving me crazy!! below is all my code. THANKS for your help!! fileupload.php----------------------------------------------------------- if($_POST['file'] != "none" && $_FILES['file']['size'] > 0) { $class=$_POST['sclass']; $description=$_POST['description']; $duedate=$_POST['duedate']; $file=$_POST['file']; $_SESSION['username']="brownka"; $id=$_SESSION['username']; $uploaddir = "C:/Apache2/htdocs/desk/submitted/"; $filename = $_FILES['file']['name']; $tmpname = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; $filetype = $_FILES['file']['type']; $filepath = $uploaddir . $filename; $result = move_uploaded_file($tmpname, $filepath); if (!$result) { echo "Error uploading file"; exit; } $insert = "insert into submitted (userid, classnum, description, filepath, filename, filesize, filetype) values ('$id', '$class', '$description', '$filepath', '$filename', '$filesize', '$filetype')"; $result = mysql_query($insert, $conn) or die('Query error:' . mysql_error()); if ($result==0) { echo "An error has occurred. Assignment was not submitted."; } else { echo "You have successfully submitted the assignment."; } } else { echo "You must select a file for upload!"; } ------------------------------------------------------------------- fdownload.php------------------------------------------------------- <?php if ($_GET['submitnum']) { include ("dbconnect.php"); $submitnum = $_GET['submitnum']; $query = "select filepath, filetype, filename, filesize from submitted where submitnum=$submitnum"; $result = @mysql_query($query, $conn); list($filepath, $filetype, $filename, $filesize) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$filename"); header("Content-length: $filesize"); header("Content-type: $filetype"); readfile($filepath); } ?> ----------------------------------------------------------- create table submitted( submitnum int not null auto_increment, userid varchar(10) not null, classnum int not null, filepath varchar (200) not null, description tinytext not null, filename varchar(50) not null, filesize varchar(50) not null, filetype varchar(50) not null, time timestamp default current_timestamp, flag int default 0 not null, primary key (submitnum), index (userid), index (classnum), foreign key (userid) references student(userid), foreign key (classnum) references classes(classnum)) ENGINE=InnoDB; Quote Link to comment https://forums.phpfreaks.com/topic/49984-solved-file-download-problem-phpmysql-help/ Share on other sites More sharing options...
brownka Posted May 4, 2007 Author Share Posted May 4, 2007 anyone have any suggestions? ive been stuck on this for a couple days now. any ideas would be great.thanks! Quote Link to comment https://forums.phpfreaks.com/topic/49984-solved-file-download-problem-phpmysql-help/#findComment-245452 Share on other sites More sharing options...
brownka Posted May 4, 2007 Author Share Posted May 4, 2007 ok ive never seen this before...apparently my include file had some white space? in it and was corrupting the file.but anyways i took it out and it works now.....ive been going crazy with this for two days fdownload.php------------------------------------------------------- <?php if ($_GET['submitnum']) { include ("dbconnect.php"); $submitnum = $_GET['submitnum']; $query = "select filepath, filetype, filename, filesize from submitted where submitnum=$submitnum"; $result = @mysql_query($query, $conn); list($filepath, $filetype, $filename, $filesize) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$filename"); header("Content-length: $filesize"); header("Content-type: $filetype"); readfile($filepath); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49984-solved-file-download-problem-phpmysql-help/#findComment-245475 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.