Analysis of the principles of php image uploading in php programming, problems related to php image uploading may occur at any time. Principles of php image uploading: First, determine whether the file type is in the image format. if so, upload the file, rename the file, and then upload the file to the specified directory, after the image is uploaded, preview the image. 1. First, determine whether the file type is an image type function {strrchr: find the last position of the string in another string. Analysis of the principles of php image uploading
In php programming, you may encounter image upload problems in php at any time.
Principles of php image uploading: First, determine whether the file type is in the image format. if so, upload the file, rename the file, and then upload the file to the specified directory, after the image is uploaded, preview the image.
1. First, determine whether the file type is a function used by the image type.
{Strrchr: searches for the last occurrence position of a string in another string, and returns all characters from this position to the end of the string. Substr: obtains some strings. $ HTTP_POST_FILES ['file'] ['name']: Get the full name of the currently uploaded file. }
The image type is the character after "." (for example, if a file name is XXX. JPG, the image type is JPG after ).
To solve the problem that the file name may be too long when uploading an image in php, you can use the php function to intercept the name of the uploaded file. Write a function to obtain the file type
2. if yes, upload the file and rename the function used by the file.
{
Strtolower: converts all letters in a string to lowercase letters.
In_array: The function searches for the given value in the array.
Implode: The function combines array elements into a string.
Random: number of randomly generated values
$ _ FILES ['userfile'] ['name']: name of the uploaded file
$ Uploaddir: a custom variable. For example, if you want to put the uploaded FILE in the same folder, you can define $ uploaddir = "./file/" in this way.
}
There will be many problems. First, write an array that can be uploaded. Second, determine the validity of the file. Third, rename the file. *
(The file size will not be written here) first define the array of types that are allowed to upload files: $ type = array ("jpg", "gif", "bmp", "jpeg ", "png"); the second uses an IF .. Else .. Write a control flow statement to determine the validity of a file.
If (! In_arry (strtolower (type (), $ type) // if there is no Upload type {$ text = implode ('. ', $ type); echo "you can only upload the following types of files:", $ text ,"
";}
The following is to rename them,
Else {$ filename = explode (". ", $ _ FILES ['userfile'] ['name']); // upload the file name to". to create an array for accuracy. $ Time = date ("m-d-H-I-s"); // The current Upload time $ filename [0] = $ time; // replace name t with name = implode (". ", $ filename); // name of the uploaded file $ uploadfile = $ uploaddir. $ name; // address of the uploaded file name}
3. finally, upload the file to the specified directory, and output the function used to preview the uploaded image after successful upload.
{Move_uploaded_file: upload file execution} if (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile) {echo"
You have uploaded the file and uploaded the image to preview it:
"; Echo"
Continue Upload
";} Else {echo" transmission failed! ";}
OK. the principles and examples of php image uploading are complete.