首页 > 解决方案 > 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;           
    }
?>

这段代码的一些主要特点是:

问题

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

标签: php

解决方案


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

推荐阅读