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 格式

问题

  1. 此代码安全吗?或者是否存在任何可能被恶意客户端利用的漏洞?在这种情况下,如何解决呢?
  2. 为了避免几个嵌套的IF-THEN-ELSE条件(可能变得难以阅读(,我目前正在使用GOTO(这可能会成为一种糟糕的控制结构实践(。有没有更好的选择?
  3. 还有其他改进的想法吗?

真的,考虑将此代码放入函数(甚至可能是类(中,而不是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; ?>

最新更新