PHP-based AJAX technology for asynchronous file upload

Source: Internet
Author: User

Asynchronous file upload is a problem that must be solved frequently in modern AJAX-implemented Web applications. However, the standard AJAX class (XmlHttpRequest) cannot implement the file transfer function. Therefore, the content discussed here is how to build an asynchronous file upload function based on AJAX technology. In this function, you need to use the built-in box and (IFRAME) to transfer files. This function enables users to use this page and enter the file description when uploading files.

This example is based on the typical case of AJAX.

System Environment

· Newer versions of browsers. For example, Opera, Firefox, or Internet Explorer.

· PHP 4.3.0 or later

· PHP version 5

· Enable the 'short _ open_tag 'option in PHP (otherwise, a parsing error will occur ).

Function Analysis

Upload files using the built-in IFRAME (framework. It consists of three parts.

· There is a simple control in the middle of the page. The target link of the form is a hidden IFRAME (implemented by the CSS style "display: none;"), and the OnChange event of the only control in the form is used to trigger JavaScript Functions. This function is used to check the extension submitted by the user and then submit the form.

· On the server side, a processing process (using FILEFRAME to sit and comment) is written in PHP ). This process is used to check the files uploaded from the client and save them on the server, and return them to the user in the form of Javascript code. The Javascript script returned to the user changed the page that the user is viewing through "parent.w.w.doc ument", set the file name, and enable the button for the user to submit the form. The enable button operation is implemented through the getElementById function.

· There is also a form on the home page that contains the description submitted by the user and the hidden file name. You can enter the description of the file while uploading the file. After the file is uploaded, click the button to view the file information returned to the user after the upload. (The file name returned and the description entered by the user constitute the file information ).

You may say that this operation does not conform to common sense: the file has been submitted before the user confirms. What if the user does not submit it. You can process the files abandoned by users by yourself.

In this example, the file is stored in the directory of a file system. You need to configure this directory when the script starts running. The specific variables that contain this directory information are $ upload_dir and $ web_upload_dir. Check whether the directory is writable.

Here we use the following PHP functions:

· Move_uploaded_file-transfers files uploaded to the server

· Fopen-open a file

· Fwrite-write content into a file

· Fclose-close the file

· Str_replace-replace string

· Filesize-returned File Size

· Filemtime-return Processing Time

You can use the manual to find these functions. Note that you should replace the HTM (<,>, &) mark with (<,> and &).

Source code

$ Upload_dir = "/var/www/anyexample/aeu"; // file storage path

$ Web_upload_dir = "/aeu"; // path of the file in the Web directory

$ Tf = $ upload_dir. '/'. md5 (rand (). ". test ";

$ F = @ fopen ($ tf, "w ");

If ($ f = false)

Die ("Fatal error! {$ Upload_dir} is not writable. Set 'chmod 777 {$ upload_dir }'

Or something like this ");

Fclose ($ f );

Unlink ($ tf );

// Process uploaded files

If (isset ($ _ POST ['fileframework'])

{

$ Result = 'error ';

$ Result_msg = 'no FILE field found ';

If (isset ($ _ FILES ['file']) // receives the file from the browser

{

If ($ _ FILES ['file'] ['error'] = UPLOAD_ERR_ OK) // No error

{

$ Filename = $ _ FILES ['file'] ['name']; // file name

Move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], $ upload_dir. '/'. $ filename );

// Main process of processing-transfer the file to $ upload_dir

$ Result = 'OK ';

}

Elseif ($ _ FILES ['file'] ['error'] = UPLOAD_ERR_INI_SIZE)

$ Result_msg = 'the uploaded file exceeds The upload_max_filesize directive in php. ini ';

Else

$ Result_msg = 'unknown error ';

}

Echo '';

Echo'

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.