【问题标题】:PHP file upload using HTTP header使用 HTTP 标头上传 PHP 文件
【发布时间】:2015-04-16 14:33:25
【问题描述】:

我有以下 PHP 代码-

    <?php

$uploaddir = getcwd().'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

我想要做的是,通过向脚本发送 HTTP 标头来上传文本文件。这是我要发送的标头-

User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------556304871068

发布内容-

-----------------------------556304871068\r\n
Content-Disposition: form-data; name="MAX_FILE_SIZE"\r\n
\r\n
512000\r\n
-----------------------------556304871068\r\n
Content-Disposition: form-data; name="userfile"; filename="example.txt"\r\n
Content-Type: text/plain\r\n
\r\n
ThisText\r\n
\r\n
-----------------------------556304871068--\r\n

但它会返回一条错误消息作为响应-

<br />
<b>Notice</b>:  Undefined index:  userfile in <b>C:\HostingSpaces\Test\mysite.com\wwwroot\admin\upload.php</b> on line <b>4</b><br />
<pre><br />

我不明白为什么会出现这个错误以及如何解决这个问题。

注意:当我正常上传文件(使用 HTML 上传页面)时,PHP 脚本运行良好。

编辑:HTML 表单代码:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

【问题讨论】:

  • html 表单在哪里?
  • 如果我做对了,您可以使用 html 表单上传文件,但在尝试使用“标题”时不能上传,您如何将标题发送到脚本?卷曲?
  • 没有。我通过“发送 HTTP 工具”发送它。我可以确认它正在发送标头。

标签: php http file-upload http-headers


【解决方案1】:

这是一个通过 POST 发送带有 php/cURL 的文件的简单脚本:

<?php
    $target_url = 'http://127.0.0.1/accept.php';
        //This needs to be the full path to the file you want to send.
    $file_name_with_full_path = realpath('./sample.jpeg');
        /* curl will accept an array here too.
         * Many examples I found showed a url-encoded string instead.
         * Take note that the 'key' in the array will be the key that shows up in the
         * $_FILES array of the accept script. and the at sign '@' is required before the
         * file name.
         */
    $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);

        $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result=curl_exec ($ch);
    curl_close ($ch);
    echo $result;
?>

这是接受文件的相应脚本。

<?php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
echo '<pre>';
    if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    echo "\n<hr />\n";
    print_r($_POST);
print "</pr" . "e>\n";
?>

【讨论】:

  • 您上面演示的示例程序是使用 curl 发送文件。但是我需要通过从客户端发送一个 HTTP 标头来做到这一点。我想我在 "$_FILES['userfile']" 部分做错了什么。
【解决方案2】:

在代码的第一行检查isset($_FILES['userfile'])

if (isset($_FILES['userfile'])) {
....
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
...
} 

//end of your file or

} else {
echo 'There is no file to send!';
}
//end of your file

【讨论】:

  • 添加这些行并发送标题后,它显示“没有要发送的文件!” :\我做错了什么?
  • 回显时尝试print_r($_FILES);
猜你喜欢
  • 2018-10-18
  • 2018-12-14
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
相关资源
最近更新 更多