简体   繁体   English

使用 php 和 wget 将文本上传为带有 POST (multipart/form-data) 的文件

[英]Upload text as file with POST (multipart/form-data) using php and wget

I'm using PHP + wget to upload a file on an old cgi-bin control panel web-to-fax.我正在使用 PHP + wget 在旧的 cgi-bin 控制面板网络到传真上上传文件。

The cgi (dont know its language) checks the file format (doc txt pdf) and then returns me the error: "incorrect file format". cgi(不知道它的语言)检查文件格式(doc txt pdf),然后向我返回错误:“文件格式不正确”。 If I do it manually everything works fine.如果我手动执行,一切正常。

The form is 6 steps long, every step is quite similar (it asks you for destination, number of retries, etc) and my PHP + wget scripts work as expected (cookies handling, post, get, etc).该表单有 6 个步骤,每一步都非常相似(它要求您提供目的地、重试次数等)并且我的 PHP + wget 脚本按预期工作(cookie 处理、发布、获取等)。

This is how the form looks like (lightened and translated from the original):这就是表格的样子(从原文变亮和翻译):

<FORM ACTION="xyz_httpfileupload" METHOD="post" ENCTYPE="multipart/form-data">
Write the file with full path:

    <INPUT TYPE="FILE" NAME="sourcefile" size=40 maxlength=200>
    <input type=hidden name=destfile value="N527yb">
    <!-- this is a random generated filename -->

    <input type=hidden name=urlredirect value="./xyz_upload?destfile=N527yb&filename=/tmp/VOTU9.txt">
    <!-- this is another random generated filename -->

    <input type=submit name="Upload" value="Upload">
</FORM>

The code (and some tests) shows that you can use both GET or POST.代码(和一些测试)表明您可以使用 GET 或 POST。

This is my code, followed by explainations:这是我的代码,然后是解释:

$command =  ' wget --debug -qSO- --cookies=on --keep-session-cookies '.
            ' --save-cookies='.$cookie.' --load-cookies='.$cookie.
            ' --header="Content-Type:text/plain" '.
            ' --post-file="'.$file.'" '.
//          ' --post-data="sourcefile='.$rawfile.'" '.
            ' --post-data="'.
                'destfile='.$randfile.
                '&urlredirect=xyz_delivery?destfile='.$randfile.
                '&filename=/tmp/'.$randname.'.txt" '.
                '&pload=Upload "xyz_httpfileupload" ';
$data = shell_exec($command." && sleep 10 2>&1 | tee ".$dir."logfile.txt");

$cookie is the full server path to the cookie file. $cookie 是 cookie 文件的完整服务器路径。 It works as expected.它按预期工作。

$file is the full server path to my file. $file 是我的文件的完整服务器路径。 It's a simple file.txt这是一个简单的file.txt

$randfile and $randname are retrieved using preg_match on the HTML output. $randfile 和 $randname 在 HTML 输出中使用 preg_match 检索。

The commented line with $rawfile is a try to send the raw urlencoded content instead of the file: somebody say it's a working alternative but not for me, or at least I didn't code it right.带有 $rawfile 的注释行是尝试发送原始 urlencoded 内容而不是文件:有人说这是一个可行的替代方案,但不适合我,或者至少我没有正确编码。

I also tried to post directly into the URL you see in urlredirect, but I get the same error file format incorrect, but I can't assure the right parsing from the cgi if I jump a step.我还尝试直接发布到您在 urlredirect 中看到的 URL,但是我得到相同的错误文件格式不正确,但是如果我跳一步,我无法确保从 cgi 中正确解析。

In any case logfile.txt was left empty.在任何情况下,logfile.txt 都是空的。

I know curl would make my life easier, but the ubuntu platform the script resides on only supports wget and I feel I'm half the way on.我知道 curl 会让我的生活更轻松,但脚本所在的 ubuntu 平台只支持 wget,我觉得我已经成功了一半。

Advice me on methods to debug it.建议我调试它的方法。 I can't via shell because I would lose the random names generated on every refresh.我不能通过 shell,因为我会丢失每次刷新时生成的随机名称。 Perhaps TCPdump?也许TCPdump? Thanks for any advice you can give me, I think this is the ultimate discussion to definitely solve this matter.感谢您给我的任何建议,我认为这是绝对解决此问题的最终讨论。

After years and pains, I concluded that CURL is the way.经过多年和痛苦,我得出结论,CURL 是方法。 This is my code snippet that works so far (BASH) to send files:这是我到目前为止(BASH)发送文件的代码片段:

formname="data=@"$HOME/samplefile.txt
$(curl -X POST -F $formname "http://www.mywebsite.com/form.php")

And a clever solution (PHP) to get those files without actually using any , which it's also good for security:一个聪明的解决方案 (PHP) 可以在不实际使用 any 的情况下获取这些文件,这也有利于安全:

$tmp_name = $_FILES["data"]["tmp_name"];
$name = "final_folder/".$_FILES['data']['name'];
move_uploaded_file($tmp_name,$name);

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM