Example of uploading, downloading, and deleting PHP files,

Source: Internet
Author: User

Example of uploading, downloading, and deleting PHP files,

The general idea of uploading, downloading, and deleting PHP files is as follows:

I. File Upload

1. Make the region of the uploaded file
Div1

2. Make the area of the display file
Div2

3. submit a form and upload a file

4. The server receives file data
Receive with $ _ FILE [name]

5. process the data to see if the uploaded file is incorrect.

There are several errors:
1) the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
2) The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
3). Only part of the file is uploaded.
4). No files are uploaded.
5). The Temporary Folder cannot be found.
6) failed to write the file.

6. Move the uploaded files from the temporary folder to the specified folder for storage.
Use this move_uploaded_file Function
Steps 4, 5, and 6 can be directly called as a function.
Note: To embed php code into a file upload page, the file extension cannot be html, But. php.

Ii. File Download

1. The client sends the file name to the server

2. The server receives the file name and adds the file path.

3. Then, send the file data back to the client.

This is generally the four steps:

// 1. reset response type $ info = getimagesize ($ rootPath. $ file); header ("Content-Type :". $ info ['mime ']); // 2. execute the downloaded file name header ("Content-Disposition: attachment; filename = ". $ file); // 3. specify the file size header ("Content-Length :". filesize ($ rootPath. $ file); // 4. response content readfile ($ rootPath. $ file );

Iii. File Deletion

1. The client sends the file name to the server

2. The server receives the file name and adds the file path.

3. Use the unlink function to delete files.

Here is a small example of uploading, downloading, and deleting images.
Effect:

The code for uploading, downloading, and deleting files is as follows:
Embedded html + php:

<! -- Select the File Upload region --> <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 a file </span> <input type = 'den den 'name = 'max _ FILE_SIZE' value = '000000'> <! -- The size of the file to be uploaded in a form is limited to <100 MB, you can also set other values --> <input type = "file" id = "upfile" name = "file"/> </div> <input type = "submit" value =" upload "class =" upload "/> </form> </div> <! -- Select the Upload File area to end --> <! -- Upload file display area --> <div id = "show-file"> <ul id = "ul-list"> <! -- Embedded php code to dynamically display uploaded files --> <? Php // 1. open the directory $ dir = opendir ('upload'); // 2. traverse the 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. Disable the directory closedir ($ dir);?> <! -- End the embedded php code --> </ul> </div> <! -- The display area of the uploaded file ends. -->

Css code:

* {Margin: 0; padding: 0;} ul, li {list-style: none;}/* div of the outermost layer, to wrap the select file button, display box and upload file button */# div1 {width: 405px; height: 38px; position: relative; margin: 40px auto ;} /* The second div wraps the display box and upload button, and floats right */# div2 {float: right;} # div2 input {width: 250px; height: 38px; font-size: 22px;}/* The third-layer div contains the 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;}/* text on the image (select file button */. text {display: block; width: 140px; height: 38px; position: absolute; top: 0; left: 0; text-align: center; line-height: 38px; font-size: 28px; color: orchid;}/* upload button position */. upload {width: 70px; height: 38px; background: greenyellow; position: absolute; top: 0; right:-75px ;} /* switch the image when you move the cursor over the select file button */# div3: hover {background: url ("upload.jpg") no-repeat 0-40px ;} /* display the div-> ul of the image. Use the left floating mode to arrange the image in one row */# 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 position and some styles */# show-file ul li div {display: none; opacity: 0; width: 40px; height: 20px; position: absolute; left: 5px; bottom: 5px; background: gold; color: # d32a0e; z-index: 1; cursor: pointer; text-align: center; line-height: 20px ;} /* Download button position and some styles */# 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 ;} /* remove the built-in style of the tag and change the font color when the mouse stays */# 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 () {// This event $ ('upfile') is triggered when a file is selected '). onchange = function () {$ ('show '). value = this. value; // uploads the obtained file pseudo path to the edit box}; // display the Download button var aLi = $ ('ul-list '). getElementsByTagName ('lil'); // image var aSpan = $ ('ul-list '). getElementsByTagName ('span '); // download the button var aDiv = $ ('ul-list '). getElementsByTagName ('div '); // Delete the 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 motion startMove (aSpan [this. index], {opacity: 100}); // buffer motion}; aLi [I]. onmouseout = function () {aSpan [this. index]. style. display = 'none'; aDiv [this. index]. style. display = 'none'; startMove (aDiv [this. index], {opacity: 0}); // buffer motion startMove (aSpan [this. index], {opacity: 0}); // buffer motion }}; function $ (id) {return document. getElementById (id) ;}</script>

Process the PHP file of the uploaded file:

Include ('myfunctions. php '); if (uploadFile ('file', 'upload') header ("Location: upFileAndDownFile. php "); // The page will jump back to the original page immediately and cannot be found here

Process the PHP file of the downloaded file:

Include ('myfunctions. php '); // GET the name of the file to be downloaded (plus the path) $ file = $ _ GET ['name']; $ rootPath = 'upload/'; downLoadFile ($ file, $ rootPath); process the PHP file for deleting the file: $ fileName = 'upload /'. $ _ GET ['name']; unlink ($ fileName); header ("Location: upFileAndDownFile. php "); move. js has been mentioned in the previous article on the JS perfect motion framework. MyFunctions. functions in php are as follows: /*** @ function download file * @ param $ file name * @ param $ rootPath root path * @ return none */function downLoadFile ($ file, $ rootPath) {// 1. reset response type $ info = getimagesize ($ rootPath. $ file); header ("Content-Type :". $ info ['mime ']); // 2. execute the downloaded file name header ("Content-Disposition: attachment; filename = ". $ file); // 3. specify the file size header ("Content-Length :". filesize ($ rootPath. $ file); // 4. response content readfile ($ rootPath. $ fi Le );} /*** @ function Upload file * @ param $ name form name <input type = "file" name = "pic"/> * @ param $ path after uploading, file Storage path * @ return the new file path, indicating that the upload is successful, false failed */function uploadFile ($ name, $ path) {$ file =$ _ FILES [$ name]; // 1. filter the uploaded file error code if ($ file ['error']> 0) {// obtain the error message switch ($ file ['error']) {case 1: $ info = 'the uploaded file exceeds php. the value restricted by the upload_max_filesize option in ini. '; Break; case 2: $ info =' the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. '; Break; case 3: $ info =' only part of the file is uploaded. '; Break; case 4: $ info =' No file is uploaded. '; Break; case 6: $ info = 'temporary folder not found'; break; case 7: $ info = 'file writing failed. '; Break;} die ("Upload error, cause :". $ info);} // 2. filter the size of uploaded files if ($ file ['SIZE']> 100000000) // bytes are measured in die ('size of uploaded files exceeds the limit! '); // 3. definition of uploaded file names $ newfile = null; $ fileinfo = pathinfo ($ file ['name']); // parse the Upload File name do {$ newfile = date ('ymdhis '). ". ". $ fileinfo ['extension'];} while (file_exists ($ path. '/'. $ newfile); // 4. execute file Upload // determine whether it is an uploaded file if (is_uploaded_file ($ file ['tmp _ name']) {// execute file upload (move the file to the specified directory) if (move_uploaded_file ($ file ['tmp _ name'], $ path. '/'. $ newfile) return $ path. '/'. $ newfile; else return false;} else die ('is not an upload file! ');}

When uploading files, you must set the HTML form size limit, server size limit, and post size limit.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.