PHP 安全的图片上传实现方法

前端之家收集整理的这篇文章主要介绍了PHP 安全的图片上传实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP安全的图片上传代码感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
这段代码用于上传图片,可以根据图片类型检测图片是否安全,不是简单的检测扩展名

/**
 * PHP安全的图片上传代码
 *
 * @param 
 * @arrange 512-笔记网: jb51.cc
 **/
<?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>";
?>
/***   来自编程之家 jb51.cc(jb51.cc)   ***/

 

猜你在找的PHP相关文章