PHP File Upload and Download Script

Here you will find file upload and download php script. First I have provided the code you can use directly and then very important tips and facts about each issues.

You will find detail descriptions of parameters, both client and server side, affecting upload and download of files. Example will show how to store MSWord file in database and also how to upload PDF file to server. Change it little bit and it will work for all types of files.

A. PHP File Upload Script


<HTML>
<HEAD>
<TITLE> PHP File Upload Script </TITLE>
</HEAD>
<BODY>
<?php
if( isset($_POST['submit1'])) {
// $_FILES is the array auto filled when you upload a file and submit a form.
$userfile_name = $_FILES['file1']['name']; // file name
$userfile_tmp  = $_FILES['file1']['tmp_name']; // actual location
$userfile_size  = $_FILES['file1']['size']; // file size
$userfile_type  = $_FILES['file1']['type']; // mime type of file sent by browser. PHP doesn't check it.
$userfile_error  = $_FILES['file1']['error']; // any error!. get from here
// Content uploading.
$file_data = '';
if ( !empty($userfile_tmp))
{
// We encode the data just to make it more database friendly
$file_data = base64_encode(@fread(fopen($userfile_tmp, 'r'), filesize($userfile_tmp) ) );
}
switch (true)
{
// Check error if any
case ($userfile_error == UPLOAD_ERR_NO_FILE):
case empty($file_data):
echo 'You must select a document to upload before you can save this page.';
exit;
break;
case ($userfile_error == UPLOAD_ERR_INI_SIZE):
case ($userfile_error == UPLOAD_ERR_FORM_SIZE):
echo 'The document you have attempted to upload is too large.';
break;
case ($userfile_error == UPLOAD_ERR_PARTIAL):
echo 'An error occured while trying to recieve the file. Please try again.';
break;
}
if( !empty($userfile_tmp))
{
// only MS office and text file is accepted.
if( !(($userfile_type=="application/msword") || ($userfile_type=="text/plain")) )
{echo 'Your File Type is:'. $userfile_type;
echo '<br>File type must be text(.txt) or msword(.doc).';
exit;
}
}
echo filesize($userfile_tmp);
}
?>
<form name="profile" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" target="_self" enctype="multipart/form-data" >
<P align ="center"><input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="file1" value="AttachFile" device="files" accept="text/*" tabindex=18 >
<input type="submit" name="submit1" value="Submit" />
</P>
</form>
</BODY>
</HTML>

Important Tips:

  • Form enctype="multipart/form-data"
  • MAX_FILE_SIZE - hidden input element with name=MAX_FILE_SIZE is used to check filesize in browser itself.
  • device="files" accept="text/*" - accept specifies mime types.
  • post_max_size, upload_max_filesize and memory_limit are php.ini settings.
  • post_max_size should be greater than upload_max_size.
  • If post_max_size is less than size of posted data then $_POST and $_FILES superglobals are empty.
  • max_input_time sets the max time in seconds, the script is allowed to receive input; this include file uploads. - also php.ini settings.
  • Check for enctype="multipart/form-data" in your form tag, if unavailable your file will not be available to your server. and you will see $_FILES empty.
  • Check name in your <input type="file">, if unavailable your $_FILES will again show nothing in it.

B. PHP File Download Script:

Download PDF file


<?php
// your file to upload
$file = '2007_SalaryReport.pdf';
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/pdf");
// tell file size
header('Content-length: '.filesize($file));
// set file name
header('Content-disposition: attachment; filename='.basename($file));
readfile($file);
// Exit script. So that no useless data is output-ed.
exit;
?>

Generate data for Excel and prompt for Downloading Excel file

Sometimes you want to give dynamically produced data to user. If you are giving dynamically generated content data to user then you have to buffer all your data until your all data is ready for download. If you do not do this then your data will not output for download correctly. or probably you will get lots of download pop-up.


<?php
// file name to be appear in output name. User can change their file name
// but this will give him a option for file name.
$file = 'testExcelFile.xls';
// start buffring
ob_start();
// sample dynamically generated data
echo '<table border="1"> ';
echo '<tr><th>Name</th><th>Age</th></tr>';
for ($i=0; $i<=5; $i++) {     echo "<tr><td>Name$i</td><td>".($i+1)."</td></tr>";
}
echo '</table>';
$content = ob_get_contents();
ob_end_clean();
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/vnd.ms-excel;charset:UTF-8");
header('Content-length: '.strlen($content));
header('Content-disposition: attachment; filename='.basename($file));
// output all contents
echo $content;
exit; // If any tags/things not supported by excel will output then it will try to //open in office word
?>

- Check data output. If you output any data to browser before header(); then header will not set.
- Check file permission if you are getting error in reading the file content.
- You must also provide link to file to be download to user so that they can download your file if for any reason your download script is not working.
<a href='http://satya61229.com/download/testfile123.pdf'>For downloading click here</a>
In this case your file will be downloaded using you provided link. The file will be opened if your file type is associated/mapped in your users browser. Otherwise they will pop-up for download.

You can download any file this way. Just change the content-type headers.
You need to change the content-type header to your file type (MIME Type).
Use application/vnd.ms-excel for excel file download, appliacation/msword for MS Word file download. If you are not sure about the file type then you can use
Content-Type:appliaction/octet-stream.

Information about How to upload large file.

Liked! So, share it.

Update: Thanks timmy for correcting the info about mime type.

  • # 1 - by Jayashree

    I have a problem with the script for download of excel file.Initially I have a page query.html which has a form with action for another script called query.php.Here the user selects one value field from mysql database and one or multiple values from another field.These pass to query.php by post method.On submission the query.php produces a tabular output.Now I want the link to enable the download of the same output as an excel file thru the script downxls.php.However it shows error in the foreach loop.I am confused how to pass the same variables again to the scipt downxls.php as were passed to query.php.

  • # 2 - by Jayashree

    I have a problem with the script for download of excel file.Initially I have a page query.html which has a form with action for another script called query.php.Here the user selects one value field from mysql database and one or multiple values from another field.These pass to query.php by post method.On submission the query.php produces a tabular output.Now I want the link to enable the download of the same output as an excel file thru the script downxls.php.However it shows error in the foreach loop.I am confused how to pass the same variables again to the scipt downxls.php as were passed to query.php.

  • # 3 - by Satya Prakash Karan

    difficult to tell anything!
    Like you was passing to query.php you can pass to download.php also. I cannot guess where are you doing mistake. It is sure that you are not doing error in excel haader etc. because you are doing error at foreach.
    Do one thing remove your header you may have put for excel and check the output, if it is correct in html format the same data can go for excel file if you put your header back.
    All Smiles!

  • # 4 - by Satya Prakash Karan

    difficult to tell anything!Like you was passing to query.php you can pass to download.php also. I cannot guess where are you doing mistake. It is sure that you are not doing error in excel haader etc. because you are doing error at foreach. Do one thing remove your header you may have put for excel and check the output, if it is correct in html format the same data can go for excel file if you put your header back. All Smiles!

Comment pages
1 2 205
Comments are open for an year period. Please, write here on Facebook page.