AJAXPHP example code for refreshing image uploads _ PHP Tutorial

Source: Internet
Author: User
AJAXPHP does not need to upload the image instance code. In the past, we used to upload files through ajax + php. This allows users to dynamically Upload images without refreshing new pages, improving the user experience, at that time, iframe was used most frequently to perform operations. Previously, we used ajax + php to upload files. This allows users to dynamically Upload images without refreshing new pages, improving user experience, at that time, iframe was used most frequently. next I will introduce the real ajax php image Upload instance.


I have been studying the no-refreshing verification of ajax + php forms, mainly for registration and submission forms. the use of ajax technology has greatly increased visitor friendliness to webpages, ajax is essential to improve page friendliness.

Of course, ajax is not only a form without refreshing verification, but also can be better applied to other places on the page. ajax technology is basically available in all areas where the form is refreshing, today we are talking about ajax + php refreshing image uploads.

The technology of refreshing images is often used to upload attachments or images, such as uploading attachments in common QQ mailboxes, uploading attachments in 163 mailboxes, and uploading images in QQ spaces, this type of data is based on the ajax refresh technology, which allows us to see uploaded attachments on the current page. in the background of a general small site, when uploading a product, the uploaded product images are often invisible, which often fails to achieve good results.

The following is the source code of a new image upload without refreshing ajax + php. this is just an example, and modifying this instance can be well applied on the webpage, add a refreshing image Upload effect to your page. you can search for it online.

File. php file

The code is as follows:





Untitled Document


$ Sort = 12;
$ F_type = strtolower ("swf, jpg, rar, zip, 7z, iso, gif"); // you can specify the file type that can be uploaded.
$ File_size_max = 200*1024*1024; // limit the maximum upload capacity of a single file
$ Overwrite = 0; // specifies whether to overwrite the same file. values: 1 (allowed) and 0 (not allowed ).
$ F_input = "Files"; // Set the name of the uploaded domain name
Foreach ($ _ FILES [$ f_input] ["error"] as $ key => $ error ){
$ Up_error = "no ";
If ($ error = UPLOAD_ERR_ OK ){
$ F_name = $ _ FILES [$ f_input] ['name'] [$ key]; // Obtain the name of the uploaded source file

$ Uploadfile = $ uploaddir. strtolower (basename ($ f_name ));

$ Tmp_type = substr (strrchr ($ f_name, "."), 1); // get the file extension
$ Tmp_type = strtolower ($ tmp_type );
If (! Stristr ($ f_type, $ tmp_type )){
Echo "script" alert ('sorry, you cannot upload ". $ tmp_type." format file, ". $ f_name." file Upload failed! ') Script ";
$ Up_error = "yes ";
}

If ($ _ FILES [$ f_input] ['size'] [$ key]> $ file_size_max ){

Echo "script" alert ('sorry, the file you uploaded ". $ f_name." capacity is ". round ($ _ FILES [$ f_input]
['Size'] [$ key]/1024). "Kb, greater than the specified". ($ file_size_max/1024). "Kb, upload failed! ') Script ";
$ Up_error = "yes ";
}

If (file_exists ($ uploadfile )&&! $ Overwrite ){
Echo "script" alert ('sorry, File ". $ f_name." already exists. Upload failed! ') Script ";
$ Up_error = "yes ";
}
$ String = 'abcdefghijklmnopgrstuvwxyz0123456789 ';
$ Rand = '';
For ($ x = 0; $ x <12; $ x ++)
$ Rand. = substr ($ string, mt_rand (0, strlen ($ string)-1), 1 );
$ T = date ("ymdHis"). substr ($ gettime [0], 2, 6). $ rand;
$ Attdir = "./file /";
If (! Is_dir ($ attdir ))
{Mkdir ($ attdir );}
$ Uploadfile = $ attdir. $ t. ".". $ tmp_type;
If ($ up_error! = "Yes") and (move_uploaded_file ($ _ FILES [$ f_input] ['tmp _ name']

[$ Key], $ uploadfile ))){


$ _ Msg = $ _ msg. $ f_name. 'upload successful n ';


}
Else {
$ _ Msg = $ _ msg. $ f_name. 'upload failed n ';
}
}

}
Echo "script window. parent. Finish ('". $ _ msg. "'); script";
?>

Fileupload.htm page

The code is as follows:





Brushless newest file upload system




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.