Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php在线发布多部分表单(localhost上一切正常)_Php_Post - Fatal编程技术网

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

从php在线发布多部分表单(localhost上一切正常),php,post,Php,Post,这是我的代码,它在localhost上输出正确的pdf文本版本,但当我把它放到网上时,它不会正确地将文件发送到类似的操作系统,也不会输出转换器文本。需要帮忙吗 <?php //Target url $url = "http://service.coolutils.com/PDF-Converter.php"; //Boundary definition $boundary = "---------------------".substr(md5(rand(0,32000)), 0,

这是我的代码,它在localhost上输出正确的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

我想你应该检查一些东西:

  • 您可以通过
    文件获取内容
    调用外部站点。因此,您的防火墙可能会阻止该请求
  • 我已经检查了你的脚本并删除了一些错误。打印出响应,然后你会得到一些信息,为什么它不起作用
  • 在我的案例中,我得到“您的文件是Doc格式的。请使用Total Doc Converter将其转换为其他格式。”

    
    

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

    您是否尝试从
    文件获取内容()中删除错误抑制(
    @
    )看看它是否给了一个错误?你检查过你的MAX POST大小和MAX文件上传大小是否大于你要上传的文件?我也会考虑把脚本中的PHP部分包装在一个检查中,看看表单是否被提交,这样它就不会试图在每次加载页面时执行文件处理代码。我试过了。正在删除(@),没有错误。文件大小还可以,我检查了两次。一切似乎都很好,因为在localhost上它是完美的o。O@justin-我已经按照你说的做了,我在脚本的同一目录上创建了文件名“filename=test.pdf”,并替换了这部分$data.=(file\u get\u contents($emri))$下线;使用$data.=(文件获取内容('test.pdf')$下线;不,我的回答不是空的,但我的回答不是应该的。你试过目标url的作用吗?!它将pdf转换为文本文件。我想使用该网站进行一些转换,我创建的网站必须这样做。我无法在我的服务器上安装任何程序,因为我无法访问它。我想用php做所有的事情(只是php),上面的代码在WAMP(localhost)上运行得很好,但在linux服务器上它没有发布文件:)我希望你理解我的问题我理解你的问题。那你应该。但是你的回答的结果是什么?可能是防火墙阻止了连接。如果防火墙阻止了连接,则两个表单都不会发布文件,但如果我制作了一个表单并将其操作设置为$destination,则会得到正确的输出
    <?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>