php file upload and download source code

Source: Internet
Author: User
Tags fread php file upload ranges

  First, File upload

Front page:

<!DOCTYPE html><Html><Head><MetaCharSet= "Utf-8"><Title> File Upload</Title></Head><Body><Div><FormMethod= "POST"Action= "upload.php"Enctype= "Multipart/form-data">User name<InputType= "Text"Name= "username"></Input><Br>File Brief Introduction<Br><TextAreaName= "Fileintro"Rows= "8"Cols= "50"></TextArea><Br> select a file to upload<Br><InputType= "File"Name= "MyFile"></Input><br> <input type= "Submit" = "Upload file" ></ input> </form></div< Span style= "color: #0000ff;" >></body>< Span style= "color: #0000ff;" ></html>       

  Background processing:

<?Php//Receive information about submitting a page transfer$name =$_post[' username '];$intro =$_post[' Fileintro '];//$_files the relevant information of 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 ();}*///File types can be restricted/*$filetype = $_files[' myfile ' [' type ']; if ($filetype! = ' image/jpg ' && $filetype! = ' application/pdf ') {echo "file type can only be jpg and PDF"; exit ();}*/if (Is_uploaded_file ($_files[' myfile ' [' Tmp_name '])) {//Dump the files into the directory you want 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 folderif (!File_exists ($userpath)){mkdir$userpath); }//Prevents the same user from uploading a file with the same name, adding 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". ;} elseecho" Upload file ". Span style= "color: #800080;" >$_files[' myfile ' [' Name ']. " Failed ";}} elseecho" Upload file ". Span style= "color: #800080;" > $FILES [' myfile '] [' name ']. " Failed ";}?                 

  Second, the file download:

Single File Download:

<?Phpfunction 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 a file existsif (!File_exists ($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. The returned fileHeader ("Content-type:application/octet-stream");//return by byte sizeHeader ("Accept-ranges:bytes");//Return file sizeHeader ("Accept-length:$file _size ");//Client Pop-up dialog box, corresponding file nameHeader ("Content-disposition:attachment;filename=".$file _name);$buffer = 1024;//Define buffers//To download the security, it is best to use the file byte read counter$file _count = 0;//Feof used to determine whether a file is read to the end of a documentwhile (!Feof$FP) && ($file _size-$file _count>0 $file _data = fread (  $fp,  $buffer ); // statistics read how many bytes  $file _count+  $buffer echo  $file _data//  $fp //}?>   

  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");//return by byte sizeHeader ("Accept-ranges:bytes");//Return file sizeHeader ("Accept-length:$file _size ");//Client Pop-up dialog box, corresponding file nameHeader ("Content-disposition:attachment;filename=".$file _name);$buffer = 1024;//Defining buffers$file _count = 0;//Feof used to determine whether a file is read to the end of a documentwhile (!Feof$FP) && ($file _size-$file _count>0) { $file _data = fread ($fp,$buffer); // Statistics read the number of 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.