php - PHP 模板:文件上传处理程序
问题描述
我正在尝试为PHP
我一直在处理的常见任务开发一些模板。其中之一是通用文件上传处理程序。
到目前为止,我正在使用以下可重用代码,它似乎工作正常,没有任何明显的错误:
<?php
if ( !isset($_POST['submit']) ) {
goto page_content;}
if ( $_FILES['file_upload']['error']===4 ) {
echo 'No file uploaded';
goto page_content;}
if ( $_FILES['file_upload']['error']===1 || $_FILES['file_upload']['error']===2 ) {
echo 'File exceeds maximum size limit';
goto page_content;}
if ( $_FILES['file_upload']['error']!==0 ) {
echo 'Failed to upload the file';
goto page_content;}
if ( !is_uploaded_file($_FILES['file_upload']['tmp_name']) ) {
echo 'Failed to upload the file';
goto page_content;}
require_once('imageResize.php');
$err = imageResize($_FILES['file_upload']['tmp_name'], 'random.png' );
if ( $err !== 0 ) {
echo 'Invalid image format';
goto page_content;}
echo 'Image uploaded successfully';
page_content:
?>
<form action="filename.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="file_upload" accept="image/*">
<input type="submit" name="submit">
</form>
附加文件imageResize.php
:
<?php
// image resize
function imageResize($source, $target){
$size = getimagesize($source);
if ($size === false) {return 1;} // invalid image format
$sourceImg = @imagecreatefromstring(@file_get_contents($source));
if ($sourceImg === false) {return 2;} //invalid image format
$width = imagesx($sourceImg);
$height = imagesy($sourceImg);
$sidelenght = min($width,$height);
$targetImg = imagecreatetruecolor(100, 100);
imagecopyresampled($targetImg, $sourceImg, 0, 0, ($width-$sidelenght)/2, ($height-$sidelenght)/2, 100, 100, $sidelenght, $sidelenght);
imagedestroy($sourceImg);
imagepng($targetImg, $target);
imagedestroy($targetImg);
return 0;
}
?>
这段代码的一些主要特点是:
- 为上传过程中可能发生的最常见错误提供消息
- 它允许客户端上传最大 1Mb 大小的图像文件
- 将所有图像调整为标准 100x100 像素大小
- 将所有图像保存为标准 PNG 格式
问题
- 这段代码安全吗?或者是否有任何漏洞可以被恶意客户端利用?在这种情况下,如何解决呢?
- 为了避免几个嵌套
IF-THEN-ELSE
条件(可能变得难以阅读),我目前正在使用GOTO
(这可能成为一种糟糕的控制结构实践)。有更好的选择吗? - 还有其他改进的想法吗?
解决方案
真的,考虑将这段代码放入函数(甚至可能是一个类)中,而不是goto
仅仅使用return
. 这将允许您更好地构建和分离需要分离的逻辑。
看这个例子:
function upload_image($file)
{
if( $err = check_error($file['error']) ) return $err;
if( !is_uploaded_file($file['tmp_name']) ) return 'Failed to upload the file';
$resize = imageResize($file['tmp_name'], 'random.png');
if( $resize !== 0 )
{
return 'Invalid image format';
}
return true;
}
对于错误检查,请查看使用该switch
功能。它将更有条理(在我看来)。
我还将在单独的函数中检查数字上传错误,这将允许轻松区分单个操作。
function check_error($err)
{
if($err === 0)
{
return false; // no errors
}
$response = false;
switch($err)
{
case 1:
case 2:
$response = 'File exceeds maximum size limit';
break;
case 4:
$response = 'No file uploaded';
break;
default:
$response = 'Unkown error';
}
return $response;
}
然后只需调用该函数并在顶部显示错误(如果有):
$upload = upload_image($_FILE['file_upload']);
if( $upload === true ):
echo 'Image uploaded successfully!';
else:
echo $upload;
?>
<form action="filename.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="file_upload" accept="image/*">
<input type="submit" name="submit">
</form>
<?php endif; ?>
推荐阅读
- shell - 在文件中查找文本
- html - Yii2:如何删除视图中的必需属性?
- javascript - 带有.each()的jQuery动态div id不起作用
- c - 可执行文件 IMAGE_OPTIONAL_HEADER ImageBase 为 0
- android - RecyclerView 尝试在空对象引用上调用虚拟方法 setAdapter
- javascript - 用php从输入框中获取值
- android - EAGAIN 何时 open() 会失败?
- python - 以相反的顺序应用 Python Pandas 指数加权平均值
- date - 解析 UTC 日期字符串并转换为不同的格式
- ssis - SSIS OLE DB 数据源输入