PHP Image Upload implementation code with detailed notes, you can learn from your favorite friends.
PHP Image Upload implementation code with detailed notes, you can learn from your favorite friends.
The Code is as follows:
// Upload the image processing file
If ($ _ FILES ["file"] ["type"] = "image/gif ") | ($ _ FILES ["file"] ["type"] = "image/jpeg ") | ($ _ FILES ["file"] ["type"] = "image/pjpeg ")) & ($ _ FILES ["file"] ["size"] <100000) {// controls the types of images that can be uploaded. The last 100000 is the size of the images that can be uploaded.
If ($ _ FILES ["file"] ["error"]> 0 ){
Echo "Return Code:". $ _ FILES ["file"] ["error"]."
"; // Error returned
} Else {
/* // This is the information of the uploaded image. You can see the effect after removing the comments.
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";*/
If (file_exists ("userupload/". $ _ FILES ["file"] ["name"]) {
Echo $ _ FILES ["file"] ["name"]. "already exists .";
} Else {
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "userupload/". $ _ FILES ["file"] ["name"]);
}
$ Date = date ('ymdhis '); // obtain the current time, for example, 20070705163148
$ FileName = $ _ FILES ['file'] ['name']; // obtain the name of the uploaded file.
$ Name = explode ('.', $ fileName); // splits the file name by '.' To get the suffix and an array.
$ NewPath = $ date. '.'. $ name [1]; // you can find a new file named '20070705163148.jpg ', which is the new path.
$ OldPath = $ _ FILES ['file'] ['tmp _ name']; // Temporary Folder, that is, the previous path
Rename ("userupload/". $ fileName, "userupload/". $ newPath );
// Here, you can write your SQL statement. The image address is "userupload/". $ newPath
?>