Page 1 of 1

file uploading and downloading

Posted: Thu Apr 07, 2005 7:04 pm
by dannymc1983
i have set up a website which allows users to upload files and then other users can view these files. the files are uploading ok (except for one issue which ill outline below). however when i try to download these files in internet explorer it fails. if for example if i try to download a word file a dialog box appears giving me the name, size and type of the file (whih are all correct) and it asks me whether i want to open the file or save it. if i click open microsoft word opens and says:
"the file cannot be found
try one or more of the following:
check the spelling or the name of the document
try a different filename
(C:\...\K1M7YVA1\file1[1].doc)".
if i click save the file is saved and then when i go to open it, it opens fine with the same filename that it was uploaded with.
if i try to open or save the files in netscape however it works fine! i have no idea why it fails in explorer yet works in netscape. i have checked the database table holding the files after they are uploaded and the filename,size and type are all stored correctly. here is the main part of the code im using for downloading:

Code: Select all

<?php

$size = //function to get file size from database;
$type = //function to get file type from database;
$name = //function to get file name from database;
$data = //function to get file data from database;

ob_end_clean();
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename = $name");
echo $data;

?>
also, i cant seem to be able to upload any files over 1 megabyte. i get no errors when i try to upload a file larger than 1mb but it does not go into the database. im using a medium blob datatype for holdingt the data so this should be more than enough for a 1 mb file.
can anyone help me out with these problems,thanks!

Re: file uploading and downloading

Posted: Thu Apr 07, 2005 10:41 pm
by anjanesh
dannymc1983 wrote:also, i cant seem to be able to upload any files over 1 megabyte. i get no errors when i try to upload a file larger than 1mb but it does not go into the database. im using a medium blob datatype for holdingt the data so this should be more than enough for a 1 mb file.
In Your php.ini settings, you may have upload_max_filesize = 1M, which is why you cannot upload more than 1 MB (I think this is why).
Try this - override php.ini settings :

Code: Select all

ini_set("upload_max_filesize","2M");

Posted: Fri Apr 08, 2005 1:52 am
by harsha
this function was taken from php manual i think you are lookin for this
function

Code: Select all

<?php

function dl_file($file){

   //First, see if the file exists
   if (!is_file($file)) { die("<b>404 File not found!</b>"); }

   //Gather relevent info about file
   $len = filesize($file);
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename,"."),1));

   //This will set the Content-Type to the appropriate setting for the file
   switch( $file_extension ) {
         case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $ctype="application/vnd.ms-excel"; break;
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
     case "gif": $ctype="image/gif"; break;
     case "png": $ctype="image/png"; break;
     case "jpeg":
     case "jpg": $ctype="image/jpg"; break;
     case "mp3": $ctype="audio/mpeg"; break;
     case "wav": $ctype="audio/x-wav"; break;
     case "mpeg":
     case "mpg":
     case "mpe": $ctype="video/mpeg"; break;
     case "mov": $ctype="video/quicktime"; break;
     case "avi": $ctype="video/x-msvideo"; break;

     //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
     case "php":
     case "htm":
     case "html":
     case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

     default: $ctype="application/force-download";
   }

   //Begin writing headers
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public"); 
   header("Content-Description: File Transfer");
   
   //Use the switch-generated Content-Type
   header("Content-Type: $ctype");

   //Force the download
   $header="Content-Disposition: attachment; filename=".$filename.";";
   header($header );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
   @readfile($file);
   exit;
}
?>
Also Check out the MAximum data that you can post using POST Method
and the increase the response time of POST Method you have to do these changes in php.in
;-)

Posted: Fri Apr 08, 2005 6:42 am
by timvw
btw, php has a mime_content_type function which is imho far more efficient than writing it yourself...

Last time i post this (i'm starting to feel like a spammer :p) http://timvw.madoka.be/programming/php/download.txt for a sample code

Posted: Fri Apr 08, 2005 3:10 pm
by dannymc1983
thanks timvw, that script worked.
as regards the other problem (trying to upload files greater than 1mb) i have set the following in php.ini:
post_max_size = 8M
upload_max_filesize = 10M
but it still wont work. i tried increasing the script execution time in php.ini also to 60 seconds but it still timed out. i even tried inserting the file directly into the table using phpmyadmin but it still timed out!
any other suggestions on what might be causing the problem???

Posted: Fri Apr 08, 2005 3:27 pm
by Chris Corbyn
dannymc1983 wrote:thanks timvw, that script worked.
as regards the other problem (trying to upload files greater than 1mb) i have set the following in php.ini:
post_max_size = 8M
upload_max_filesize = 10M
but it still wont work. i tried increasing the script execution time in php.ini also to 60 seconds but it still timed out. i even tried inserting the file directly into the table using phpmyadmin but it still timed out!
any other suggestions on what might be causing the problem???
The webserver itself restricts file sizes too (1MB seems to be the unlikely value too). You may want to check this. It's a topic that's been discussed on this forum many times before if you have a little search :D

Posted: Fri Apr 08, 2005 4:17 pm
by dannymc1983
i found in one post there is a directive called limitrequestbody in apache that is set to 512k which may be stopping me form uploafing large files. supposodly this directive can be found in php.conf or httpd.conf. i cant find it in httpd.conf and i cant even find a file called php.conf. anyone know anything about this directive?

Posted: Fri Apr 08, 2005 5:46 pm
by timvw
meaby a stupid question, but i'm going to ask it anyway:
i read you changed the php.ini, but have you also restarted your webserver ?(otherwise changes are not loaded...)

Posted: Fri Apr 08, 2005 5:49 pm
by Chris Corbyn
timvw wrote:meaby a stupid question, but i'm going to ask it anyway:
i read you changed the php.ini, but have you also restarted your webserver ?(otherwise changes are not loaded...)
Changes to php.ini are registered w/out rebooting the server. Aren't they? I just thought it was changes to the web server that needed a reboot.

Maybe that's just a windows thing...

Posted: Fri Apr 08, 2005 5:50 pm
by John Cartwright
Nope, you gotto reboot

Posted: Fri Apr 08, 2005 5:53 pm
by timvw
i didn't mean restart your computer :)

just stop and start apache (or restart/reload if you have that option) or whatever webserver software you are using...

Posted: Fri Apr 08, 2005 5:58 pm
by Chris Corbyn
I know you didn't mean restart the computer...

On my windows box running PHP4 and Apache 2 I make changes to php.ini without restarting the server. It works too - like adding extensions and stuff :? I guess my PC is just a magic one :P

Posted: Sat Apr 09, 2005 12:57 am
by dannymc1983
i made that mistake in my first few days of writing php code, but not any more! so that aint the problem...