Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am writing a code in php for downloading a file .It is working but it is downloading my php file rather then the selected file to download.
I have created download.php in which i have written my code.below is my code

PHP
<?php
if(isset($_REQUEST['download']))
{
//$path = BASE_URL."/pdf/";
//$filename= $path.basename($_GET['download_file']);
$filename=$_POST['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;filename='.basename($filename));
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;
}
?>
<html><form>
<input type="file" name="file" /><input type="submit" name="download" value="download" /></form>



.By this code i am just downloading my this code only.But i want to download a file which is browsed by input type=file.

Please help
Posted
Updated 26-May-14 5:25am
v2
Comments
Sunasara Imdadhusen 21-May-14 1:55am    
Have searched on Google?
Rage 22-May-14 8:54am    
What does $filename contains ?

Your uploading code is not correct, see example:
XML
<?php
if(isset($_POST['download']))
{
$filename=$_FILES['file']['tmp_name'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;filename='.basename($filename));
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;
}
?>
<html>
<body>
<form method="post" enctype="multipart/form-data" action="download.php">
<input type="file" name="file" >
<input type="submit" name="download" value="download">
</form>
</body>
</html>

Note the extra attributes in the <form> tag, and the use of $_FILES not $POST to capture the file object in php. PHP will give the upload file a temporary file name called $_FILES['file']['tmp_name'] which will disappear when the script ends.
Read more:
1. php_file_upload[^]
2. Beginner's Guide to HTML5 & CSS3 - Server Side Story[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900