Jump to content

php mysql file download problem-file downloads as scriptname


brownka

Recommended Posts

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!";

}

 

}

--------------------------------------------------------------------------------

Link to comment
Share on other sites

Try...

 

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=". basename($filename));
header("Content-Description: File Transfer");

Link to comment
Share on other sites

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;

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.