本文實例講述了PHP安全上傳圖片的方法。分享給大家供大家參考。具體分析如下:
這段代碼用於上傳圖片,可以根據圖片類型檢測圖片是否安全,不是簡單的檢測擴展名
<?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>"; ?>
希望本文所述對大家的php程序設計有所幫助。