PDF上载程序脚本

时间:2012-10-08 13:41:16

标签: php security pdf upload file-rename

我花了3天的时间阅读并做了关于PHP上传脚本的教程,用于将PDF文件上传到网站,重命名和移动文件。我似乎无法找到关于如何格式化脚本的一致答案。从我的阅读中我看到以下对这个过程很重要。我没有找到PDF上传问题的良好工作答案。

  1. 使用服务器端mime验证验证PDF文件非常重要。
  2. Clamscan对于保持场地清洁非常重要。这应该在上传后立即完成吗?
  3. 在按类型和无病毒验证文件之前,无法访问这些文件。我的理解是,这最好通过PHP的“重命名”功能来完成,这将允许移动和重命名。
  4. 我曾考虑将PDF上传到我的MySql数据库,但在阅读了大部分在线建议后,我现在觉得这对我的网站来说可能不是最好的。

  5. 我将让客户通过html表单上传pdf文件。

  6. 以下是我使用的表格:

        <form action ="PHP UPLOAD.php" method="post" encrypte="multipart/form-data">
        File: <input type="file" name="file" size="30"> <input type="submit" value="upload!">
        </form>
    

    这是我从在线教程开始的PHP。

    <?php
    
    $uploaddir = "cover";
    $allowed_ext = "pdf";
    $max_size = "5000000";
    //$max_height = "";
    //$max_width = "";
    
    $extension = pathinfo($_FILES['file'] ['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $allowed_ext);
    for($i = 0; $i < count($allowed_paths); $i++) {
    if ($allowed_paths [$i] == "$extension") {
        $ok = "1";
    }
    }
    
    
    if ($ok == "1")  {
        if($_FILES['file']['size'] > $max_size)
        {
            print "File is too big!";
            exit;
        }
    
    // Include for images
    //if ($max_width && $max_height) {
    //list ($width, $height, $type, $w) =
    //getimagesize($_FILES['file']['tmp_name']);
    //if ($width . $max_width || $height > $max_height)
    //{
    //print "File height and/or width are too big!";
    //exit;
    //}
    //}
    
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    print "Your Cover Letter has been successfully uploaded!";
    } else {
    print "Incorrect file extension!";
    }
    
    ?>
    

    它向我返回了“无效的文件类型”消息。

    我找到了这段代码,它似乎是通过Mime Type更好的验证,但似乎并不完整。

    $allowedExts = array(
      "pdf", 
      "doc", 
      "docx"
    ); 
    
    $allowedMimeTypes = array( 
      'application/msword',
      'text/pdf',
      'image/gif',
      'image/jpeg',
      'image/png'
    );
    
    $extension = end(explode(".", $_FILES["file"]["name"]));
    
    if ( 20000 < $_FILES["file"]["size"]  ) {
      die( 'Please provide a smaller file [E/1].' );
    }
    
    if ( ! ( in_array($extension, $allowedExts ) ) ) {
      die( 'Please provide another file type [E/2]. );
    }
    
    if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) ) 
    {      
     move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); 
    }
    else
    {
    die( 'Please provide another file type [E/3]. );
    }
    

    我正在努力学习,但我似乎无法找到解决我问题的非常明确的道路。有人可以给我一些明确的答案,说明PDF上传和这个过程的特殊之处吗?

    对于背景,我正在使用MAMP和Dreamweaver&amp; amp;文本牧马人。我将把它实现到GoDAddy网站。所以,如果你知道我将面临的问题,我会很感激。

    提前致谢!

2 个答案:

答案 0 :(得分:3)

我认为错误消息是因为您错过键入的enctype作为加密,请检查此内容

<form action ="PHP_UPLOAD.php" method="post" enctype="multipart/form-data">
File: 
<input type="file" name="file" size="30"> 
<input type="submit" value="upload!">
</form>

答案 1 :(得分:0)

嗨,第二个通过Mime Type进行验证的代码,但似乎不是这样完成的。

$allowedExts = array(
  "pdf", 
  "doc", 
  "docx"
); 

$allowedMimeTypes = array( 
  'application/msword',
  'application/pdf',
  'text/pdf',
  'image/gif',
  'image/jpeg',
  'image/png'
);

$nameexploded = explode(".", $_FILES["file"]["name"]);
$extension = end($nameexploded);

if ( 9000000 < $_FILES["file"]["size"]  ) {
  die( 'Please provide a smaller file [E/1].' );
}

if ( ! ( in_array($extension, $allowedExts ) ) ) {
  die( 'Please provide another file type [E/2].' );
}

if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) ) 
{      
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); 
}
else
{
    die( 'Please provide another file type [E/3].' );
}

我使用$ nameexploded来避免: 注意:在第16行的filepath \ test.php中,只能通过引用传递变量。

这对我很有用。希望帮助某人。