1. Uploading a picture three steps
The first step : First determine whether the file type is a picture format, if you upload files;
The second step : And then rename the file (generally to avoid uploading the file name, and now basically is the time to be named);
The third step : Finally upload the file to the specified directory, successfully uploaded after the output upload image preview.
2. Code (1) Determine whether the file type is a picture type
1 <? PHP 2 function type () {3 return substr (strrchr($HTTP _post_files[' file '] [' name '], '. '), 1); 4 }5 ?>
Description
- STRRCHR: Finds the position of the last occurrence of a string in another string and returns all characters from that position to the end of the string.
- SUBSTR: Takes a partial string.
- $HTTP _post_files[' file ' [' name ']: Gets the full name of the file currently being uploaded.
(2) If the picture, upload the file, and then rename the file according to the time
1 if(!in_arry (Strtolower(Type ()),$type)){ 2 $text=implode(‘.‘,$type); 3 Echo"You can only upload the following types of files:",$text, "<br>"; 4}Else{5 $filename=Explode(".",$_files[' UserFile '] [' name ']);//put the uploaded file name as "." Number to make an array. 6 $time=Date("M-d-h-i-s");//to the current upload time7 $filename[0]=$time;//take file name T replace8Name=implode(".",$filename);//the file name after uploading9 $uploadfile=$uploaddir.$name;//the file name address after uploadingTen}
This step is also divided into three steps:
First, write an array of types that can be uploaded, $type=array("JPG", "gif", "BMP", "JPEG", "PNG");
Then, judge the legitimacy of the document;
Finally, duplicate the file name.
Description
- Strtolower: Converts all the letters of a string to lowercase letters.
- In_array: The function searches the array for the given value.
- Implode: function combines array elements into a single string
- Random: Randomly generated numbers
- $_files[' UserFile ' [' name ']: Upload file name
- $uploaddir: A variable that you define yourself. For example, in the same folder, you want to put the uploaded files in this folder in the file folder, you can define $uploaddir="./file/"; Note the wording
- $_files["File" ["Name"]-the name of the file being uploaded
- $_files["File" ["type"]-the type of file being uploaded
- $_files["File" ["Size"]-the size of the uploaded file, measured in bytes
- $_files["File" ["Tmp_name"]-the name of the temporary copy of the file stored on the server
- $_files["File" ["Error"]-error code caused by file upload
(3) Finally upload the file to the specified directory, the upload Image preview after successful uploading
1 if(Move_uploaded_file($_files[' UserFile '] [' Tmp_name '],$uploadfile)){2 Echo"<center> your file has been uploaded upload picture preview: </center><br><center>$uploadfile' ></center> '; 3 Echo"<br><center><a href= ' Javascrīpt:history.go ( -1) ' > Continue uploading </a></center>"; 4}Else{5 Echo"Transmission failed!" "; 6}
The move_uploaded_file () function is a PHP system function that moves uploaded files to a new location.
Syntax: move_uploaded_file(file, Newloc)
Note : This function is only used for files uploaded via HTTP POST.
Note : If the destination file already exists, it will be overwritten.
Also attached:w3school php File Upload
Three steps to upload a picture in PHP