Home >Backend Development >PHP Tutorial >How to safely upload images in PHP, _PHP tutorial

How to safely upload images in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:03968browse

How to upload images safely with PHP,

This article describes the method of uploading images safely with PHP. Share it with everyone for your reference. The specific analysis is as follows:

This code is used to upload images. It can detect whether the image is safe based on the image type, not simply detecting the extension

<&#63;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>";
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970976.htmlTechArticleHow to upload images safely in PHP. This article describes the method of uploading images safely in PHP. Share it with everyone for your reference. The specific analysis is as follows: This code is used to upload pictures, you can...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn