Download Files

Hello guys!!

I really need help with this one. The principal idea is to download files, for the moment any type of files(images, pdf, doc,…)

I almost have the full code (it works. I Upload a file and its show it in the table) however is missing the download part, I know I have to use the header something:

//header(‘Content-Disposition: attachment; filename=’. basename($folder));
//readfile($folder);

but where and how I have to do it that I dont know.

thanks.

This is the first part html file.

<!doctype html>
<html>
<head>
<title>...</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/$$$.css">
<script src="js/.js"></script>
</head>
<body>
<h1>Files</h1>
<form action="files.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"></p>
<p>
<input type="submit" value="Upload" />

<input type="hidden" name="submitted" value="TRUE" />
</p>
</form>
</body>
</html>

the second and import part

[php]
$folder=‘file/’;

if( !is_dir($folder) ){
die (“the folder not exits!”);
}

echo “

”;
print_r($_FILES);
echo “

”;

if (isset($_POST[‘submitted’])) {

if (isset($_FILES[‘file’])) {

//if (strpos($_FILES[‘file’][‘type’], “doc”) || strpos($_FILES[‘file’][‘type’], “pdf”) ) {}

//if (in_array($_FILES[‘file’] [‘type’], $allowed)) {
// Move the file over.
if (move_uploaded_file($_FILES[‘file’][‘tmp_name’],
“$folder{$_FILES[‘file’][‘name’]}”)) {
echo ‘

The file has been uploaded!

’;
} // End of move… IF.
	 } else  { // Invalid type.
		 echo '<p class="error">Please upload a pdf or doc file.</p>';
		 }
		
		 } // End of isset($_FILES['upload']) IF.
		
		 // Check for an error:
		
		 if ($_FILES['file']['error'] > 0) {
		  echo '<p class="error">The file couldnot be uploaded because: <strong>';
		 	
		 	 // Print a message based upon the error.
		 	 switch ($_FILES['file']['error']) {
		 		 case 1:
		 		 print 'The file exceeds the upload_max_filesize setting in php.ini.';
		 		 break;
		 		 case 2:
		 		 print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
		 		 break;
		 		 case 3:
		 		 print 'No file was uploaded.';
		 		 break;
		 		 case 4:
		 		 print 'No temporary folder was available.';
		 		 break;
		 		 default:
		 		 print 'A system error occurred.';
		 		 break;
		 	 } // end of switch  
		 	 print '</strong></p>';
		 	 
		 } // End of error IF.
		 
			 		  if (file_exists
			 		  ($_FILES['file']['tmp_name']) &&
			 		  is_file($_FILES['file']['tmp_name']) ) {
			 		  	 unlink
			 		  	($_FILES['file']['tmp_name']);
			 		  	 }

// } // End of the submitted conditional.
?>

<?php ////////////////////////////////////////////////////////////// //header('Content-Disposition: attachment; filename='. basename($folder)); //readfile($folder); $list=scandir($folder); if( !$list ){ die("Error in the file system"); } echo "" . ""; foreach ($list as $arc){ echo ""; } echo "
Name link
$arc Download
"; ////////////////////////////////////////////////////////////// ?>

[/php]

Well, first, you can’t easily download all types of files. Websites that serve up, let’s say PDF files will usually open them up. So, the user must know they have to RIGHT-CLICK on it to save it. Make sure you tell them somewhere on the page…

Now, a download is just a HREF to the file. It has to include the PATH to the file. If your path and filename is correct, then it can be downloaded. So, a simple way to see where the error is, is to use a trick I have told many many people on this site. Bring up the page which should have the links on it for downloading. Then, RIGHT-CLICK on a blank area of the page and select VIEW-SOURCE option. This will show you the actual code after the PHP is gone. Then, you can see the file download HREF and see what is wrong with it. Most likely it is something small, but, it may be the path to the file. Of course, ask further questions if you get stuck…

Hello!!

First thanks for the help

Ok. This is my result

I upload and image and is set to the table.

image.png----download

I select the download option and then show the image in a new window (web browser)

this is the view source code:


<p>Array
(
    [file] => Array
        (
            [name] => image1.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\...\....tmp
            [error] => 0
            [size] => 888784
        )

)
</p><p><em>The file has been uploaded!</em></p>
<table border='1'><tr><th>Name</th><th>link</th></tr><tr><td>.</td><td><a <tr><td>image1.jpg</td><td><a href='file/image1.jpg'>Download</a></td></tr></table>

Download
the path of the file is correct, right ?: the a href file match with the file array.

This is one of the solutions of my problem ;D. Now my question, to not open a new window can be used the “header code” ?. but how…

thanks

Okay, to explain file downloading again. You can not set up a link to automatically download a file if the filetype is PDF or JPG or other types. Browsers are set up to open files not download them. If they are a ZIP file or a file with extension .zip, then they will be downloaded. Now, with that said… You can add a display on your download page to tell people to RIGHT-CLICK on a file and select DOWNLOAD from the option list. This is usually enough for them to know to do it that way. The other option is to zip-up all your files. But, then they have to unzip the files and this is bothersome. Here is another explanation of all this:

http://webdesign.about.com/od/beginningtutorials/qt/html_dwnld_tag.htm

Now, your code for pulling the name from a folder and displaying it was nearly correct. In the code section:
[php]
foreach ($list as $arc){
echo “

$arc Download ”;
}
[/php]
The folder and file ($arc) was not separated correctly. You would have to change this to something like this:
[php]
foreach ($list as $arc){
echo “ $arc Download ”;
}
[/php]
That formats the folder and archived file with the correct slash between them.
Hope that helps, if not ask any further questions… Good luck.

Ok! Thanks

Let me try one more time

Sponsor our Newsletter | Privacy Policy | Terms of Service