Home > Article > Backend Development > How to upload images in PHP and check if they are safe
This article mainly introduces the method of uploading images safely in PHP. It can detect the image type and realize the function of safely judging the image. It is of great practical value. Friends in need can refer to it.
The examples in this article describe PHP security How to upload images. Share it with everyone for your reference. The specific analysis is as follows:
This code is used to upload pictures. It can detect whether the picture is safe according to the picture type. It is not a simple detection of the extension
<?php // upload.php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method='post' action='upload2.php' enctype='multipart/form-data'> Select a JPG, GIF, PNG or TIF File: <input type='file' name='filename' size='10' /> <input type='submit' value='Upload' /></form> _END; if ($_FILES) { $name = $_FILES['filename']['name']; switch($_FILES['filename']['type']) { case 'image/jpeg': $ext = 'jpg'; break; case 'image/gif': $ext = 'gif'; break; case 'image/png': $ext = 'png'; break; case 'image/tiff': $ext = 'tif'; break; default: $ext = ''; break; } if ($ext) { $n = "image.$ext"; move_uploaded_file($_FILES['filename']['tmp_name'], $n); echo "Uploaded image '$name' as '$n':<br />"; echo "<img src='$n' />"; } else echo "'$name' is not an accepted image file"; } else echo "No image has been uploaded"; echo "</body></html>"; ?>
Summary: The above is The entire content of this article is hoped to be helpful to everyone's study.
Related recommendations:
Principles and usage of encryption and decryption in php
Principles and usage of encryption and decryption in php
phpTwo commonly used methods to recursively delete folders
The above is the detailed content of How to upload images in PHP and check if they are safe. For more information, please follow other related articles on the PHP Chinese website!