PHP image Upload implementation code with detailed notes
Source: Internet
Author: User
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
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