douben8492 2016-01-22 07:51
浏览 334
已采纳

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

I've made a form that I want the user to submit as an entire PDF to be saved on the server. I've been trying to make a php script that handles the files coming in. Basically, I've taken the example from W3 schools and tried to adapt it:

<?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.";
    }
}
?>

However, it's not working. I get the second error message: "Sorry, there was an error uploading your file."

I'm fairly new to php and would greatly appreciate some help.

Thanks!

UPDATE: All the documentation and examples involve POST data from an HTML form, so the input field name is known in each. My PDFs are generated server side with random names, so I had to adapt the examples. I made this one which worked with an HTML form:

<?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)
?>

However, when I submitted from the PDF itself using Adobe Acrobat, I got a series of errors:

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

Although my script uploads files from HTML forms without needing to know the input field name (which is always known in the documentation examples), which I was pleased about, it doesn't work when submitted from within the PDF itself.

Does anyone know why that would be?

  • 写回答

1条回答 默认 最新

  • douzhouqin6223 2016-01-22 21:08
    关注

    If you submit a PDF form by sending the whole document, you just have to take the raw post data as the document data. There are no individual post fields but:

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

    ...is enough. You have the whole PDF document in $fileContent now. So just save this (after validation!) and you're done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”