从php在线发布多部分表格(localhost上一切正常)

时间:2012-03-28 15:15:23

标签: php post

这是我的代码,在localhost上输出正确的文本版本的pdf,但是当我把它放在网上时,它并没有正确发送文件,这样的东西并没有ouptu转换文本。请需要一些帮助

<?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 - 我简化了代码,使其更加清晰。我在localhost上使用WAMP(win32),它的工作情况各不相同。问题是它在linux服务器上无法正常工作。

文件“test.pdf”位于脚本的同一文件夹中,所以任何人都会说问题就在那里,不要说:D

1 个答案:

答案 0 :(得分:1)

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

  1. 您通过file_get_contents呼叫外部网站。因此,防火墙可能会阻止请求。
  2. 我检查了你的脚本并删除了一些错误。打印出响应然后你得到一些信息,说明它无法正常工作。
  3. 我的情况我得到了“您的文件是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为空时,你应该检查你与其他服务器的连接。