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 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!