Implementation of file asynchronous uploading based on PHP Ajax technology

Source: Internet
Author: User
Tags file size parse error

Asynchronous file uploads are frequently encountered and must be addressed in modern AJAX-enabled Web applications. But the standard Ajax class (XMLHttpRequest) does not implement the ability to transfer files. So what we're talking about here is how to build asynchronous file uploads on the basis of Ajax technology. In this function, you need to use the built-in boxes and (IFRAME) to transfer files. The effect of this function is that when the page uploads the file, the user can also use the page and fill in the file description.

This example is an analysis of the classic examples of Ajax that we cite.

System environment

· Newer version of the browser. such as Opera,firefox or Internet Explorer.

· PHP 4.3.0 or later

· PHP 5 version

· The ' short_open_tag ' option in PHP is turned on (otherwise a parse error will occur).

Functional analysis

Files are uploaded through the built-in iframe (frame). Consists of three parts.

· There is a simple control in the middle of the page. This form's target link is a hidden iframe (through the CSS style "display:none;" Implementation) and the onchange event for the only control within the form to trigger the JavaScript function. The function is to check the extension that the user submits, and then submit the form.

· A processing process was written on the server side using PHP (Fileframe sat annotated). 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 changes the page that the user is currently viewing through "parent.window.document", sets the name of the file, and enables the button for the user to submit the form. The action to enable the button is implemented through the getElementById function.

· There is also a form on the main page that contains the user-submitted description and the hidden file name. The user can fill in the description of the file while uploading the file. When the file is uploaded, the user clicks on the button and can see the file information returned to the user after uploading. (form file information by returning a filename and a description of the user input).

You might say it's not common sense to do this: The file has been submitted before the user confirms it. What would happen if the user did not submit it? You can handle files that are discarded by the user at the extension.

This example stores the file in a directory on a file system. You need to configure this directory when the script starts to run, and the specific variables that contain this directory information are $upload_dir and $web_upload_dir. Here is a permission check for whether the directory is writable.

Here we use the following PHP functions:

· Move_uploaded_file-Transfer once uploaded to server file

· fopen-Open File

· Fwrite-writing content to a file

· Fclose-Close File

· Str_replace-Replacement string

· FileSize-Returns the file size

· Filemtime-Return processing time

You can find these functions in the manual if used. Please note that you want to replace the HTM (&) tags with (., > and &).

Source

$upload _dir = "/var/www/anyexample/aeu"; Path to file storage

$web _upload_dir = "/aeu"; The path of the file under 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[' fileframe '))

{

$result = ' ERROR ';

$result _msg = ' No FILE field found ';

if (isset ($_files[' file '))//Accept file from browser

{

if ($_files[' file '] [' error '] = = UPLOAD_ERR_OK)//No Error

{

$filename = $_files[' file ' [' Name ']; Filename

Move_uploaded_file ($_files[' file '] [' tmp_name '], $upload _dir. '/'. $filename);

Main process of processing-transfer files 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.