您好我正在尝试使用多部分将PDF文件上传到服务器,但它给了我错误。我所做的是创建一个上传页面,用户上传PDF文件和I会话目标文件并将其发送到下面的代码将其发送到服务器我在做任何错误下面的任何帮助将不胜感激。这是索引页面。
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload file" name="submit">
</form>
这是我将目标文件发送到的代码。
<?php
session_start();
$file = $_SESSION ["data"];
?>
<?php
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
define('file', 'fileToUpload');
$filename = $file;
$content = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".file."\";
filename=\"".basename($filename)."\"\r\n";
// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"foo\"\r\n\r\n".
"bar\r\n";
// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--\r\n";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
$result = file_get_contents('//link', false,
$context);
print_r($result);
?>