>  기사  >  php教程  >  PHP安全上传图片的方法

PHP安全上传图片的方法

WBOY
WBOY원래의
2016-06-06 20:06:10875검색

本文实例讲述了PHP安全上传图片的方法。分享给大家供大家参考。具体分析如下:

这段代码用于上传图片,可以根据图片类型检测图片是否安全,不是简单的检测扩展名

<?php // upload.php
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method=&#39;post&#39; action=&#39;upload2.php&#39; enctype=&#39;multipart/form-data&#39;>
Select a JPG, GIF, PNG or TIF File:
<input type=&#39;file&#39; name=&#39;filename&#39; size=&#39;10&#39; />
<input type=&#39;submit&#39; value=&#39;Upload&#39; /></form>
_END;
if ($_FILES)
{
$name = $_FILES[&#39;filename&#39;][&#39;name&#39;];
switch($_FILES[&#39;filename&#39;][&#39;type&#39;])
{
case &#39;image/jpeg&#39;: $ext = &#39;jpg&#39;; break;
case &#39;image/gif&#39;: $ext = &#39;gif&#39;; break;
case &#39;image/png&#39;: $ext = &#39;png&#39;; break;
case &#39;image/tiff&#39;: $ext = &#39;tif&#39;; break;
default: $ext = &#39;&#39;; break;
}
if ($ext)
{
$n = "image.$ext";
move_uploaded_file($_FILES[&#39;filename&#39;][&#39;tmp_name&#39;], $n);
echo "Uploaded image &#39;$name&#39; as &#39;$n&#39;:<br />";
echo "<img src=&#39;$n&#39; />";
}
else echo "&#39;$name&#39; is not an accepted image file";
}
else echo "No image has been uploaded";
echo "</body></html>";
?>

希望本文所述对大家的php程序设计有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.