2016-01-22 37 views
1

我做了一个表格,我希望用户提交作为整个PDF保存在服务器上。我一直在试图让一个PHP脚本,来处理在文件基本上,我已经从学校W3拍摄的榜样,努力去适应它:处理PDF格式提交作为整个pdf使用PHP

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

然而,当我使用的Adobe 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本身提交时不起作用。

有谁知道这是为什么?

+1

看起来您正在混合上传文件的两种方法,并且完全不执行任何操作。只需使用文档中的示例:http://php.net/manual/en/features.file-upload.post-method.php –

回答

0

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

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

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