Jump to content

[SOLVED] file download problem (php/mysql) HELP


brownka

Recommended Posts

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;

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);

 

}

?>

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.