1
votes

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
Looks like you are mixing two methods of uploading a file and do none of them completely. Just use the examples from the documentation: php.net/manual/en/features.file-upload.post-method.phpGerald Schneider

1 Answers

0
votes

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.