从 php 在线发布多部分表单(本地主机上一切正常)

问题描述 投票:0回答:2

这是我的代码,在本地主机上输出正确的 pdf 文本版本,但是当我将其放在网上时,它不会正确发送文件,也不会输出转换文本。请需要一些帮助

<?php


//Target url
$url = "http://service.coolutils.com/PDF-Converter.php";


//Boundary definition
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);

//Post data
$data = "";

//Fields
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"ConvertTo\"\n\ntxt\n";
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"Converter\"\n\npc\n";
$data .= "--$boundary\n";

//Files
$fileContents = file_get_contents('test.pdf');
$data .= "Content-Disposition: form-data; name=\"filename\"; filename=\"test.pdf\"\n";
$data .= "Content-Type: application/pdf\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary\n";

//Header
//$optional_headers = header('Content-Type: multipart/form-data; boundary='.$boundary);



//Construct params
$params = array('http' => array(
 'method' => 'POST',
 'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
 'content' => $data
));

//Create context
$ctx = stream_context_create($params);

//Post data to url
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
 echo "Error posting to $url: " . $php_errormsg;
 exit(1);
}

//Read response
$response = @stream_get_contents($fp);
if ($response === false) {
 throw new Exception("Problem reading data from $url, $php_errormsg");
 exit(1);
}

//Display response (with a little bit of formatting)
$response = strip_tags($response);
$response = preg_replace('/\s+\n/', "\n", $response);
$response = preg_replace('/\n\s+/', "\n", $response);
$response = preg_replace("'[ ]+'", ' ', $response);
echo $response;

echo "Done";
?>

更新 14/04 - 我简化了代码以使其更加清晰。我在本地主机上使用 WAMP (win32),它工作得很好。问题是它不能在Linux服务器上在线运行。

文件“test.pdf”位于脚本的同一文件夹中,因此任何人如果说有问题就不要说出来:D

php post
2个回答
2
投票

我认为你应该检查一些事情:

  1. 您通过
    file_get_contents
    呼叫外部站点。因此您的防火墙可能会阻止该请求。
  2. 我检查了你的脚本并删除了一些错误。打印出响应,然后您会得到一些它不起作用的信息。

我的情况是“您的文件是 Doc 格式。请使用 Total Doc Converter 将其转换为其他格式。”

<?php
ini_set('max_execution_time', 300);

$tmp = $_FILES['filename']['tmp_name']; 
$emri = $_FILES['filename']['name'];
$madhesia= $_FILES['filename']['size'];
$file_src = dirname(__FILE__)."/uploads/";

if (move_uploaded_file($tmp, $file_src.$emri)) {
    echo 'Skedari u ngarkua me sukses.';
} else {
    echo 'Ndodhi nje problem ne ngarkim!';
}

if(isset($_POST['submit'])) {
    $destination = "http://service.coolutils.com/PDF-Converter.php";

    $eol = "\r\n";
    $data = '';

    $mime_boundary=md5(time());

    $data .= '--' . $mime_boundary . $eol;
    $data .= 'Content-Disposition: form-data; name="ConvertTo"' . $eol . $eol;
    $data .= "txt" . $eol;
    $data .= '--' . $mime_boundary . $eol;
    $data .= 'Content-Disposition: form-data; name="Converter"' . $eol . $eol;
    $data .= "pc" . $eol;
    $data .= '--' . $mime_boundary . $eol;
    $data .= 'Content-Disposition: form-data; name="filename"; filename='.$emri . $eol;
    $data .= 'Content-Type: application/pdf' . $eol;
    $data .= 'Content-Transfer-Encoding: binary' . $eol . $eol;
    $data .= file_get_contents($file_src.$emri) . $eol;
    $data .= "--" . $mime_boundary . "--" . $eol . $eol; 

    $para = array('http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: multipart/form-data; boundary=' . $mime_boundary . $eol,
        'length' => 'Content-Length: '.$madhesia,
        'content' => $data
    ));

    $ctx = stream_context_create($para);

    $response = file_get_contents($destination, FILE_USE_INCLUDE_PATH, $ctx);

    /**
     * Debug
     */         
    var_dump($response);
}
print_r($response);
?>
<form name="MainForm" action="#" enctype="multipart/form-data" method="post">
    <input type="Hidden" name="ConvertTo" value="txt" />
    <input id='albi' type="file" size="50" name="filename" />
    <input name="submit" type="submit" />
</form>

希望对您有所帮助,并能找到问题所在。但是当 $response 为空时,您应该检查与其他服务器的连接。


0
投票

11 年后为那些可能觉得这有帮助的人回答我自己的问题。问题是本地主机在 Windows 上,而在线服务器是 Linux。 Windows 和 Linux 具有不同的 EOL 规则。 Windows 使用 /r/n 分割新行,而 Linux 仅使用 /n 字符。在执行“低级”操作(例如自己格式化标题)时要非常小心。让图书馆做这些事情或者至少把它做好。每当需要时都使用 PHP_EOL,因为它可以处理 Linux 和 Windows 中行结尾的差异。

© www.soinside.com 2019 - 2024. All rights reserved.