Ajax & amp; PHP: Learn and practice 5 image processing. 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. For file upload, you only need a Form to explain in the previous article how to submit a Form through Ajax and use PHP to process the underlying data. This article will mainly introduce the upload and processing of images. It is easy to upload files. you only need a Form to upload the source files to the target directory through PHP. First:
Create a Form in Sample6_1.php:
Copy to ClipboardReference: [www.bkjia.com] // displays the upload status and image
// Enctype needs to be defined for file uploading. set target to uploadframe for image display.
Upload image function uploadimg:
Copy to ClipboardReference: [www.bkjia.com] 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:
Copy to ClipboardReference: [www.bkjia.com] // 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>