php file upload, download and delete sample _php tips

Source: Internet
Author: User
Tags file upload html form php code php file upload readfile

php file upload, download and delete examples of general ideas are as follows, the specific contents are as follows

I. File upload

1. Make the area of the uploaded file
Div1

2. Make the area of the display file
Div2

3. Submit the form, upload the file

4. server receives file data
Receive with $_file[name]

5. Processing data to see if there are errors in uploading files

There are several errors:
1). The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini
2. The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form
3. File only partially uploaded
4). No files are uploaded
5). Temporary folder not found
6). File Write Failed

6. Move the uploaded files from the temporary folder to the designated folder store
With this move_uploaded_file function
Where 4 5 6 steps can be made into a function called directly.
Note: File upload page If you want to embed PHP code, the file name extension can not be HTML, but. php

Two. File download

1. The client sends the file name to the server

2. The server receives the filename and then adds the path to the file.

3. Then transfer the file data back to the client

Generally are these four steps:

   1. Reset response type
  $info = getimagesize ($rootPath. $file);
  Header ("Content-type:". $info [' MIME ']);
  2. Execute the downloaded filename
  header ("Content-disposition:attachment;filename=". $file);
  3. Specify file size
  header ("Content-length:". FileSize ($rootPath. $file));
  4. Response Content
  ReadFile ($rootPath. $file);

Three. File deletion

1. The client sends the file name to the server

2. The server receives the filename and then adds the path to the file.

3. Use the Unlink function to perform the delete file operation

Here is a picture upload download to delete a small example.
Effect as shown:

File upload download Delete the interface, the code is as follows:
html+php Inline:

<!--Select Upload file area--> <div id= "Div1" > <form action= "uploadfile.php" "method=" Post "enctype=" multipart/
      Form-data "> <div id=" div2 "><input type=" text "id=" show "/></div> <div id=" Div3 "> <span class= "Text" > select File </span> <input type= ' hidden ' name= ' max_file_size ' value= ' ' 100000000 ' > <! --the form upload file size limit <100m, you can also set other values--> <input type= "file" id= "Upfile" name= "file"/> </div> <in Put type= "Submit" value= "Upload" class= "upload"/> </form> </div> <!--Select Upload file area end--> <!--upload file display area- 
    -> <div id= "Show-file" > <ul id= "ul-list" > <!--inline PHP code to dynamically display uploaded files--> <?php. Open Directory
    $dir = Opendir (' upload ');
    2. Traverse directory $i = 0; while ($file = Readdir ($dir)) {if ($file = = '. ')
        $file = = '.. ')
      Continue echo "<li> <div><a href= ' deletefile.php? name={$file} ' > Delete </a></span></div> <span><a href= ' download.php?name={$file} ' > download
    </a></span></li> ";
    //3. Close directory Closedir ($DIR);
 ?> <!--inline PHP code end--> </ul> </div> <!--upload file display area end-->

CSS code:

*{margin:0;padding:0;}
    Ul,li{list-style:none;}

    /* Outermost Div, the purpose is to wrap the Select File button, display box and upload file button * * #div1 {width:405px;height:38px;position:relative;margin:40px auto;}
    /* Second Layer div wrap display box and upload button, right float * * * #div2 {float:right;}

    #div2 input {width:250px;height:38px;font-size:22px;}
      /* The third layer of div wrapped input file*/#div3 {float:left;width:140px;height:38px;position:relative;
    Background:url ("upload.jpg") no-repeat 0 0;margin-left:5px;}
      #div3 input{position:absolute;width:100%;height:100%;top:0;left:0;

    z-index:1;opacity:0;}
      /* Picture (Select File button) on the text/* TEXT{DISPLAY:BLOCK;WIDTH:140PX;HEIGHT:38PX;POSITION:ABSOLUTE;TOP:0;
      left:0;text-align:center;line-height:38px;font-size:28px;

    Color:orchid;}

    /* The position of the upload button/* upload{width:70px;height:38px;background:greenyellow;position:absolute;top:0;right: -75px;}

    /* Mouse to stay in the selection of the file button on the time to switch the picture * * * #div3: Hover{background:url ("upload.jpg") No-repeat 0-40px;} /* Display the Div->ul of the picture, using the way of the left float, row of the arrangement of pictures * * * #show-File{width:760px;height:445px;position:relative;margin:10px Auto;overflow:scroll;}
    #show-file ul{width:760px;height:445px;position:absolute;top:0;left:0;}

    #show-file ul li{float:left;width:120px;height:100px;margin:3px 0 0 3px;position:relative;} /* Delete button location and some style * * #show-file ul Li Div{display:none;opacity:0;width:40px;height:20px;position:absolute;left:5px;bo
      ttom:5px;

    Background:gold;color: #d32a0e; z-index:1;cursor:pointer;text-align:center;line-height:20px;} /* The location of the download button and some style * * #show-file ul Li span{display:none;opacity:0;width:40px;height:20px;position:absolute;right:5px;
      bottom:5px;

    Background:gold;color: #d32a0e; z-index:1;cursor:pointer;text-align:center;line-height:20px;}
    * * To remove a label's own style, mouse stop when the font for color * * #show-file ul Li Span,div a{text-decoration:none;color:orangered;

 #show-file ul li Span,div a:hover{color: #00fa00;}

JS Code:

 <script src= "Move.js" ></script> <script> window.onload = function () {//When a file is selected, this event is triggered
      $ (' upfile '). onchange = function () {$ (' show '). Value = this.value;//Upload the obtained file pseudo path to the edit box};   Show Download button var aLi = $ (' ul-list '). getElementsByTagName (' Li '); Picture var Aspan = $ (' ul-list '). getElementsByTagName (' span ');  Download button var Adiv = $ (' ul-list '). getElementsByTagName (' div ');
        Delete button for (var i = 0;i<ali.length;i++) {ali[i].index = i;
          Ali[i].onmousemove = function () {aspan[this.index].style.display = ' block ';
          Adiv[this.index].style.display = ' block '; Startmove (adiv[this.index],{opacity:100}); Buffer Movement Startmove (aspan[this.index],{opacity:100});
        Buffer movement};
          Ali[i].onmouseout = function () {Aspan[this.index].style.display = ' none ';
          Adiv[this.index].style.display = ' None '; Startmove (Adiv[this.index],{opacity:0});  Buffer Movement Startmove (aspan[this.index],{opacity:0});
    Buffer Movement}}};
    function $ (ID) {return document.getElementById (ID);

 } </script>

PHP files for processing uploaded files:

Include (' myfunctions.php ');
if (uploadfile (' file ', ' upload '))
  header ("Location:upFileAndDownFile.php");/will immediately jump back to the original page, do not feel the page has jump to here

PHP files for processing downloaded files:

Include (' myfunctions.php ');
Gets the file name (plus path) to be downloaded $file = $_get[' name '];
$rootPath = ' upload/';

DownLoadFile ($file, $rootPath);
The PHP file that handles the delete file: $fileName = ' upload/'. $_get[' name '];
Unlink ($fileName);

Header ("Location:upFileAndDownFile.php"); 
Which move.js in front of the JS perfect motion frame article has been talked about. The functions in myfunctions.php are as follows:/** * @function Download file * @param $file the file name to download * @param $rootPath file root Path * @return No */function
  DownLoadFile ($file, $rootPath) {//1. Reset response Type $info = GetImageSize ($rootPath. $file);
  Header ("Content-type:". $info [' MIME ']);
  2. Execute the downloaded filename header ("content-disposition:attachment;filename=". $file);
  3. Specify File Size header ("Content-length:". FileSize ($rootPath. $file));
4. Response Content ReadFile ($rootPath. $file); /** * @function Upload file * @param $name table single-name <input type= "file" name= "pic"/> * @param $path uploaded, files stored path * @retur
  n returns the new file path to indicate that the upload succeeded false/function UploadFile ($name, $path) {$file = $_files[$name]; 1. filter upload file Error number if ($file [' Error '] > 0) {//Get error message switch ($file [' ERROR ']{Case 1: $info = ' uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
        ';
      Break Case 2: $info = ' the size of the upload file exceeds the value specified by the Max_file_size option in the HTML form.
        ';
      Break Case 3: $info = ' file is only partially uploaded.
        ';
      Break Case 4: $info = ' no files are uploaded.
        ';
      Break
        Case 6: $info = ' temporary folder not found ';
      Break Case 7: $info = ' File write failed.
        ';
    Break
  } die ("Upload error, Cause:". $info);
  //2. upload file size filter if ($file [' Size '] > 100000000)//Bytes die (' Upload file size exceeds limit! ');
  3. After uploading the filename definition $newfile = null; $fileinfo = PathInfo ($file [' name ']); Resolves the upload filename do{$newfile = date (' Ymdhis ').
  $fileinfo [' extension ']; }while (file_exists, $path. '
  /'. $newfile)); 4. Execute File Upload//judge whether it is an upload file if (is_uploaded_file ($file [' tmp_name '])) {//Execute File Upload (move file to specified directory) if (move_uploaded_file ($file [' Tmp_name '], $path. " /'. $newfile)) return $path. '
    /'. $newfile;
  else return false;
Else die (' not an upload file! ');

 }

When uploading files, be aware that you want to set the HTML form size limit and the size limit of the server, post size limit.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.