首页 文章

PDF上载程序脚本

提问于
浏览
2

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

  • 使用服务器端mime验证验证PDF文件非常重要 .

  • Clamscan对于保持场地清洁非常重要 . 这应该在上传后立即完成吗?

  • 文件在按类型和无病毒验证之前不应该可访问 . 我的理解是,这最好由PHP的"rename"函数完成,它允许移动和重命名 .

  • 我曾考虑将PDF上传到我的MySql数据库,但在阅读了大部分在线建议后,我现在觉得这对我的网站来说可能不是最好的 .

  • 我将让客户通过html表单上传pdf .

这是我使用的表单:

<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&Text Wrangler在我的MAC上构建它 . 我将把它实现到GoDAddy网站 . 所以,如果你知道我将面临的问题,我会很感激 .

提前致谢!

2 回答

  • 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>
    
  • 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中的引用传递 .

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

相关问题