PHP and JavaScript_PHP tutorials

Source: Internet
Author: User
Tags file upload progress bar
Tracking Upload progress PHP and JavaScript. A problem that has plagued Web developers for many years is how to add to their applications, such as file Upload progress bars and real-time information. Users are impatient; they don't want to wait, and a question that has plagued Web developers for years is how to add them to their applications, such as file Upload progress bars and real-time information. Users are impatient; they don't want to wait, while browsers are doing something, wondering if they have been frozen or if they have a slow connection. A progress indicator is provided to provide users with useful information and let them know what happened.
First of all, you may want to make it possible to easily obtain the file size from your computer for the first time, then, execute the file in which the directory is located to perform some simple calculations on the uploaded server. With regard to the second idea, you will find that things are not that simple.
JavaScript can access the file name, type, or even the width and height of the local image, but it is still HTML5, and it can access the file size. Unfortunately, HTML5 is still not a complete standard and is evenly distributed across all browsers. An alternative solution relies on a Flash, Java, or ActiveX plug-in. no thanks, I will pass. However, another solution is to install the optional PHP Cache extension, but it may not depend on your hosting environment. it seems like such a small task, such as overhead.
It seems that all the options are full of troubles, and the task has quickly become a headache. But in Yuda, "No... is another ."
One of the many reasons I love PHP is that it makes seemingly difficult tasks easy. In PHP 5.4, they have made another new configuration instruction set, session. upload_progress.
In this article, I will tell you how this function can be used to create a simple Upload progress bar without any external library or browser dependencies. I will first discuss how it works, and then I walk through the four files you created and the tasks to be completed (the upload form, some JavaScript, a little bit of CSS, returns the uploaded status and file ).
Session Upload progress
In addition to the general requirements, you can upload files with two tracking steps. You must enable the session. upload_progress.enabled command and the webpage form with the name you specified must have a hidden session. upload_progress.name command. When session. upload_progress.enabled is true (because it is in PHP 5.4 by default, probably exceeds) $ _ POST [session. upload is sent during upload_progress.name, and the file transmission information is an ultra-global array in $ _ SESSION.
The output $ _ SESSION array of print_r () will be similar to the following file transfer process:

Array
(
[Upload_progress_myForm] => Array
(
[Start_time] => 1323733740
[Content_length] = & gt; 721127769
[Bytes_processed] = & gt; 263178326
[Done] =>
[Files] => Array
(
[0] => Array
(
[Field_name] => userfile
[Name] => ubuntu-10.04.3-desktop-i386.iso
[Tmp_name] =>
[Error] => 0
[Done] =>
[Start_time] => 1323733740
[Bytes_processed] = & gt; 263178026
)
)
)
)

When you are developing a local or fast network to upload small files, you won't be able to intuitively observe the progress because transfer occurs so fast. In this case, you may want to try to transfer a large file. Make sure that the setting file in your php. ini allows uploading large, specific post_max_size upload_max_filesize commands, and then confirm that they are rational value when you go to production.

Create form

I will propose the first file upload form. Just to keep things as simple as possible, such as posting to yourself, only one file can be uploaded at a time. In addition, I will not disturb the upload after saving the file.
The following code is form. php:
01
02
If ($ _ SERVER ["REQUEST_METHOD"] = "POST "&&! Empty ($ _ FILES ["userfile"]) {
03
// Move_uploaded_file ()
04
}
05
?>
06

07

08
File Upload Progress Bar
09

10

11

12


13


14


15


16

23

24

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.