5. process images while learning and practicing Ajax + PHP. First create Form in Sample6_1.php: Copy the code as follows: to display the upload status and image divdiv Upload File, define enctype. in order to display the image, first create Form:
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>