php file upload and download source code, _php tutorial

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

php file upload and download source code,


  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= " the"> 
        textarea > < BR > Select File to upload <br><input  type= "File"  name= "myfile"> 
         input><br  > <input  type= "Submit"  value= "Upload file" > 
          input >  
           form> 
            div> 
             body >  
              html>       

  Background processing:

 Php//receive information about submitting a page transfer$name=$_post[' username '];$intro=$_post[' Fileintro '];//information about files in $_files//echo "
";//Print_r ($_files);//echo"
";//You can limit the size of the file 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 folder if(!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".$_files[' MyFile '] [' name ']. " Success; }Else{ Echo"Upload file".$_files[' MyFile '] [' name ']. " Failed; }}Else{ Echo"Upload file".$FILES[' MyFile '] [' name ']. " Failed;}?>

  Second, the file download:

Single File Download:

 
 PhpfunctionDown_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 exists    if(!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 file    Header("Content-type:application/octet-stream"); //return by byte size    Header("Accept-ranges:bytes"); //return file Size    Header("Accept-length:$file _size"); //Client Pop-up dialog box, corresponding file name    Header("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 document     while(!feof($fp) && ($file _size-$file _count>0)){    $file _data=fread($fp,$buffer); //How many bytes did the statistic read ?    $file _count+$buffer; Echo $file _data; //send some data to the browser    }    fclose($fp); //Close File    }?>

  Multiple file Downloads:

downlist.php:

Down

Down

Down

Down

  downprocess.php:

 Php$filename=$_request[' filename ']; functionDown_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 size    Header("Accept-ranges:bytes"); //return file Size    Header("Accept-length:$file _size"); //Client Pop-up dialog box, corresponding file name    Header("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 document     while(!feof($fp) && ($file _size-$file _count>0)){        $file _data=fread($fp,$buffer); //How many bytes did the statistic read ?        $file _count+$buffer; Echo $file _data; //send some data to the browser        }    fclose($fp); } down_file ($filename);?>

http://www.bkjia.com/PHPjc/1131344.html www.bkjia.com true http://www.bkjia.com/PHPjc/1131344.html techarticle php file upload and download source code, a File upload front page:! DOCTYPE HTML HTML head meta charset = "Utf-8" title file upload/Title/head body div Form method = "Post" a ...

  • 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.