Example of uploading, downloading, and deleting php files

Source: Internet
Author: User
This article describes in detail examples of uploading, downloading, and deleting php files, if you are interested, you can refer to the following general ideas for uploading, downloading, and deleting php files:

I. file Upload

1. make the region of the uploaded file
P1

2. make the area of the display file
P2

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:

Embedded html + php:

 

    Delete

    Download";}// 3. close the directory closedir ($ dir);?>

Css code:

* {Margin: 0; padding: 0;} ul, li {list-style: none;}/* p on the outermost layer, to wrap the Select File button, display Box and upload File button */# p1 {width: 405px; height: 38px; position: relative; margin: 40px auto ;} /* The second layer p includes the display box and upload button, and the right float */# p2 {float: right;} # p2 input {width: 250px; height: 38px; font-size: 22px;}/* The third layer p contains the input file */# p3 {float: left; width: 140px; height: 38px; position: relative; background: url ("upload.jpg ") no-repeat 0 0; margin-left: 5px; }# p3 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 */# p3: hover {background: url ("upload.jpg") no-repeat 0-40px ;} /* Display the p-> ul of the image. The Left float is used 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 p {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, p a {text-decoration: none; color: orangered ;} # show-file ul li span, p a: hover {color: #00fa00 ;}

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