Home  >  Article  >  php教程  >  PHP安全上传图片的方法

PHP安全上传图片的方法

WBOY
WBOYOriginal
2016-06-06 20:06:10875browse

本文实例讲述了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程序设计有所帮助。

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