5 Image processing for learning and practicing Ajax + PHP

Source: Internet
Author: User
The previous article explains how to submit a form through Ajax and process underlying data by PHP. This article mainly describes how to upload and process images. It is easy to upload files. you only need a Form to upload the source files to the target directory through PHP. Ajax

First:


Create a Form in Sample6_1.php:
The code is as follows:
// Display the upload status and image


// Enctype needs to be defined for file uploading. set target to uploadframe for image display.


Upload image function uploadimg:
The code is as follows:
Function uploadimg (theform ){
// Submit the Form
Theform. submit ();
// In showimg

Displays the upload status
SetStatus ("Loading...", "showimg ");
}
// Upload status function
Function setStatus (theStatus, theObj ){
Obj = document. getElementById (theObj );
If (obj ){
Obj. innerHTML ="

"+ TheStatus +"

";
}
}

Process_upload.php provides the file upload function:
The code is as follows:
// Provides image type verification
$ Allowedtypes = array ("image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/gif ");
// File storage directory
$ Savefolder = "images ";

// Start to work if a file is uploaded
If (isset ($ _ FILES ['myfile']) {
// Check whether the uploaded file meets the $ allowedtypes type
If (in_array ($ _ FILES ['myfile'] ['type'], $ allowedtypes )){
If ($ _ FILES ['myfile'] ['error'] = 0 ){
$ Thefile = "$ savefolder/". $ _ FILES ['myfile'] ['name'];
// Upload a file through move_uploaded_file
If (! Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], $ thefile )){
Echo "There was an error uploading the file .";
}
Else {
?>
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>


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.