First, File upload
Front page:
<! DOCTYPE html>
Background processing:
<?php//receive information about the delivery of the submitted page $name = $_post[' username '); $intro = $_post[' Fileintro '];//$_files information about the file//echo "<pre >//Print_r ($_files);//echo "</pre>";//You can limit the file size here/* $filesize = $_files[' myfile ' [' Size '];if ($ filesize>2*1024*1024) {echo "file too large to upload"; Exit ();} *///can restrict the file type/* $filetype = $_files[' myfile ' [' type ']; if ($filetype! = ' image/jpg ' && $filetype! = ' application/pdf ') {echo "file type can be JPG and PDF only"; Exit (); }*/if (is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {//dump the file to the directory you wish to store $uploaded = $_files[' myfile ' [' Tmp_name ' ]; Each user dynamically creates a folder $userpath = $_server[' Document_root '). " /up/". $name; Determine if the user already has a folder if (!file_exists ($userpath)) {mkdir ($userpath); }//Prevent the same user from uploading a file with the same name, you can add a timestamp to the file. $moveto = $userpath. " /". Time (). $_files[' myfile ' [' name ']; or modify the file name, but you need to use string processing to intercept the filename suffix $truename = $_files[' myfile ' [' name ']; $moveto = $userpath. " /". Time (). substr ($truename, Strrpos ($truename,". ")); if (MoVe_uploaded_file ($uploaded, Iconv ("Utf-8", "gb2312", $moveto)) {echo "Upload file". $_files[' myfile ' [' Name ']. " Success "; }else{echo "Upload file". $_files[' myfile ' [' Name ']. " Failure "; }}else{echo "Upload file". $FILES [' myfile '] [' name ']. " Failed ";}? >
Second, the file download:
Single File Download:
<?php function Down_file ($file _name, $file _path) {//$file _name = Iconv ("Uft-8", "gb2312", $file _name); If the file name is in Chinese, the Chinese name needs to be transcoded gb2312//The file to be downloaded reads into the server's memory//the server returns the file data to the browser//browser writes the file to the user-specified location//1. Determine if the file exists if (!file_exist S ($file _name)) {echo "111"; return; } $fp = fopen ($file _name, "R"); $file _size = filesize ($file _name); Get file Size//can limit browser download file size via file_size. Returns the header of the file ("Content-type:application/octet-stream"); Returns the header by byte size ("Accept-ranges:bytes"); Returns the file size header ("Accept-length: $file _size"); The Client Popup dialog box, corresponding to the filename header ("content-disposition:attachment;filename=". $file _name); $buffer = 1024; Define buffers//In order to download the security, it is best to use the file byte read counter $file _count = 0; Feof is used to determine if a file is read to the end of the document while (!feof ($fp) && ($file _size-$file _count>0)) {$file _data = fread ($fp, $buffer); Statistics read how many bytes $file _count+ $buffer; echo $file _data; Send some data to the browser} fclose ($FP); Close file}?>
Multiple file Downloads:
downlist.php:
<a href= "downprocess.php?filename=1.jpg" >down</a> <br><br><a href= "downprocess.php?filename=2.jpg" >down</a><br><br><a href= "downprocess.php?filename=3.jpg" >down</a><br><br><a href=" Downprocess.php?filename=4.jpg ">down</ A><br><br>
downprocess.php:
<?php $filename = $_request[' filename ']; function Down_file ($file _name) { if (!file_exists ($file _name)) { echo "111"; return; } $fp = fopen ($file _name, "R"); $file _size = filesize ($file _name); Header ("Content-type:application/octet-stream"); Returns the header by byte size ("accept-ranges:bytes"); Returns the file size header ("Accept-length: $file _size"); The Client Popup dialog box, corresponding to the filename header ("Content-disposition:attachment;filename=". $file _name); $buffer = 1024x768; Defines the buffer $file _count = 0; The feof is used to determine if a file is read to the end of the document while (!feof ($fp) && ($file _size-$file _count>0)) { $file _data = fread ($fp, $buffer); Statistics read how many bytes $file _count+ $buffer; echo $file _data; Send some data to the browser } fclose ($FP); } Down_file ($filename);? >
php file upload and download source code