【问题标题】:handle pdf form submitted as entire pdf using php使用php处理作为整个pdf提交的pdf表单
【发布时间】:2016-01-22 07:51:17
【问题描述】:

我制作了一个表单,希望用户将其作为整个 PDF 提交以保存在服务器上。我一直在尝试制作一个 php 脚本来处理传入的文件。基本上,我已经从 W3 学校中获取了示例并尝试对其进行调整:

<?php
$file = file_get_contents("php://input");
$target_dir = "uploads/";
$target_file = $target_dir . basename($file);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($file, $target_file)) {
        echo "The file ". basename( $file). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

但是,它不起作用。我收到第二条错误消息:“抱歉,上传您的文件时出错。”

我是 php 的新手,非常感谢一些帮助。

谢谢!

更新:所有文档和示例都涉及来自 HTML 表单的 POST 数据,因此每个输入字段名称都是已知的。我的 PDF 是用随机名称在服务器端生成的,所以我不得不修改这些示例。我制作了这个与 HTML 表单一起使用的表单:

<?php
foreach ($_FILES as $name => $value)
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES[$name]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES[$name]["tmp_name"], $target_file)
?>

但是,当我使用 Adob​​e Acrobat 从 PDF 本身提交时,我收到了一系列错误:

http://localhost/save.php[22/01/2016 20:23:05]
Notice: Undefined variable: name in C:\xampp\htdocs\save.php on line 4
Notice: Undefined index: in C:\xampp\htdocs\save.php on line 4
Notice: Undefined variable: target_dir in C:\xampp\htdocs\save.php on line 4
Notice: Undefined variable: name in C:\xampp\htdocs\save.php on line 7
Notice: Undefined index: in C:\xampp\htdocs\save.php on line 7

虽然我的脚本从 HTML 表单上传文件而不需要知道输入字段名称(在文档示例中总是已知的),这让我很高兴,但从 PDF 本身提交时它不起作用。

有人知道为什么会这样吗?

【问题讨论】:

标签: php forms pdf upload handle


【解决方案1】:

如果您通过发送整个文档来提交 PDF 表单,您只需将原始帖子数据作为文档数据。没有单独的帖子字段,但是:

$fileContent = file_get_contents("php://input");

...就够了。您现在在 $fileContent 中拥有整个 PDF 文档。所以只需保存这个(在验证之后!)你就完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2011-01-29
    • 2012-06-25
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多