使用 php 处理作为整个 pdf 提交的 pdf 表单

handle pdf form submitted as entire pdf using php

我制作了一个表单,希望用户将其作为完整的 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.";
    }
}
?>

但是,它不起作用。我收到第二条错误消息:"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 本身中。

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

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

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

……够了。您现在在 $fileContent 中拥有整个 PDF 文档。所以只要保存这个(验证后!)就大功告成了。