PHP模板:文件上传处理程序

时间:2018-07-04 15:58:41

标签: 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. 还有其他改善建议吗?

1 个答案:

答案 0 :(得分:1)

真的,可以考虑将此代码放入函数(甚至是一个类)中,而不是使用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; ?>
相关问题