First:
Create a form in sample6_1.php:
CopyCode The Code is as follows: // display the upload status and Image
<Div id = "showimg"> </div>
// Enctype needs to be defined for file uploading. set target to uploadframe for image display.
<Form ID = "uploadform" Action = "process_upload.php" method = "Post"
Enctype = "multipart/form-Data" target = "uploadframe">
Upload a file: <br/>
<Input type = "file" id = "myfile" name = "myfile"/>
// Upload a file
<Input type = "Submit" value = "Submit" onclick = "uploadimg (document. getelementbyid ('uploadform'); Return false;"/>
<IFRAME id = "uploadframe" name = "uploadframe" src = "process_upload.php" class = "noshow"> </iframe>
</Form>
Upload image function uploadimg:Copy codeThe Code is as follows: function uploadimg (theform ){
// Submit the form
Theform. Submit ();
// Display the upload status in showimg <div>
Setstatus ("loading...", "showimg ");
}
// Upload status Function
Function setstatus (thestatus, theobj ){
OBJ = Document. getelementbyid (theobj );
If (OBJ ){
OBJ. innerhtml = "<Div class = \" bold \ ">" + thestatus + "</div> ";
}
}
Process_upload.php provides the file upload function:Copy codeThe Code is as follows: <? PHP
// 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 using move_uploaded_file
If (! Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], $ thefile) {
echo "There was an error uploading the file. ";
}< br> else {
?>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
" OnLoad=" doneloading (parent, ' ') ">
}< BR >?>
the doneloading function in the last part of the code above is used to display the image and modify the image size. Thumb. php will be used, and the size of the source image will be generated in the images directory. If you are interested, you can study it. You are welcome to make a brick ~
package and download the source code