WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Force file download
Categories : PHP, Filesystem, HTTP, Program Execution Click here to Update Your Picture
bastien koert
Date : Jun 16th 2005
Grade : 3 of 5 (graded 7 times)
Viewed : 15426
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

The following code takes a file name as a parameter and then forces the browser to download that file.

<?
function force_download($file)
{
   
$dir      = "../log/exports/";
    if ((isset(
$file))&&(file_exists($dir.$file))) {
       
header("Content-type: application/force-download");
       
header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       
header("Content-Transfer-Encoding: Binary");
       
header("Content-length: ".filesize($dir.$file));
       
header('Content-Type: application/octet-stream');
       
header('Content-Disposition: attachment; filename="' . $file . '"');
       
readfile("$dir$file");
    } else {
       echo
"No file selected";
    }
//end if

}//end function
?>



Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem
Remote File Size
Categories : PHP, Filesystem, HTTP, Sockets
Remote File Saving with PHP - Download and serve a remote file. The content of the file will be updated at fixed intervals.
Categories : PHP, Filesystem, Cache, Sockets, HTTP
Easy upload class
Categories : PHP Classes, Filesystem, HTTP, PHP
How to let a user download a picture by clicking on it instead of needing to right click and Save-As.
Categories : HTTP, PHP, HTML and PHP, Filesystem
Execute a command to a string
Categories : PHP, Program Execution, Filesystem
Using php3 to upload files, uploading files, file uploads.
Categories : PHP, Filesystem, HTTP
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Introduction to Language Files
Categories : PHP, Filesystem, Beginner Guides
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
php jump urls...the best way
Categories : PHP, URLs, Filesystem
Simple way to replace a variable value in a .conf (.ini) file using a webbrowser - the first stage of a complete universal configuration editor
Categories : PHP, Regexps, Code Editors, Filesystem
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
Simple Password example
Categories : PHP, Authentication, Security, HTTP
 Jose Santos wrote : 1328
This is a very good code example, and very useful to download or export to file !!
 
 walid yasen wrote : 1363
hi i use that code but have all this error, and i`m sure its form me , can yu help me why that error happen.
"Warning: Cannot modify header information - headers already sent by (output started at /home/../..../.../fdownload.php:1) in /home/.../../../fdownload.php on line 8

Warning: Cannot modify header information - headers already sent by (output started at /home/.../.../..../fdownload.php:1) in /home/.../.../.../fdownload.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /home/..../.../.../fdownload.php:1) in /home/.../.../.../fdownload.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/../../.../fdownload.php:1) in /home/.../../../fdownload.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/.../.../.../fdownload.php:1) in /home/.../.../../fdownload.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/.../.../.../fdownload.php:1) in /home/../../../fdownload.php on line 13
 
 berber berberon wrote : 1364
Looks like you have a space somewhere in your file sent before the header info is sent. look for empty lines in the begining of your code.
 
 dean williams wrote : 1379
why does everyone use &lt;?  ...  ?&gt;

This is NOT correct and does NOT work on all servers, please use the PHP standards...
&lt;?php    ...     ?&gt;

Anyone having problems can try that...
 
 xgrapher xxx wrote :1688
I plopped the code on a php which I call upon from page, eg: xgrapher.com/download.php?file=test.mpg and it worked perfect. Thanks!

However, if I went back and tried downloading another file, it would simply terminate the file which is being downloaded.

I guess its because the variables are reserved till the download is completed. Could you please help?

Thanks,
xgrapher