douchuopiao9002
2014-08-21 10:06 阅读 173
已采纳

需要php代码来存储用wget上传的文件

I need to upload file on server from command line. I do next:

wget -d --post-file=links.txt http://bearnova.com/upload_file.php

Here is upload_file.php . Code below doesn't work of course:

<?php
if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];

  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

}
?>

Wget does an HTTP POST to send file. I've also heard about $_POST, $HTTP_RAW_POST_DATA and file_get_contents(php://input) I'm not a php guy at all. So could someone please help me to get a valid solution? Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    duanpang1987 duanpang1987 2014-08-21 11:23

    I get contents of file this way:

    <?php $postdata = file_get_contents("php://input"); ?>
    

    $HTTP_RAW_POST_DATA - the same as the above but depends on php.ini (empty in my case)

    I need to store it in file on server side and it seems that filename was not sent either but it should be not a big deal.

    点赞 评论 复制链接分享
  • donglaohua1671 donglaohua1671 2014-08-21 10:13

    Please check, In the html form you defined enctype or not

    <form method="post" action="upload_file.php" enctype="multipart/form-data">
        <input type="file" name="file" id="file"/>
    </form>
    

    If its not defined then also it might be not working. As, for file field this enctype in form is needed.

    点赞 评论 复制链接分享
  • dongzhi6146 dongzhi6146 2014-08-21 10:14

    wget can't be used for file uploads. File uploads require that the content type be multipart/form-data, but wget doesn't support this. From the man page:

    Wget does not currently support multipart/form-data' for transmitting POST data; only application/x-www-form-urlencoded'

    --post-file just posts ordinary form fields, it just gets the values from the contents of the file.

    You can do what you want with curl instead.

    curl -F file=@links.txt http://bearnova.com/upload_file.php
    
    点赞 评论 复制链接分享
  • dqxafj6830 dqxafj6830 2014-08-21 11:58

    This is how you can do it with wget.

    wget -d --post-data="filedata=`base64 links.txt`&filename=links.txt" http://bearnova.com/upload_file.php
    

    you can get filename and filedata from upload_file.php, and do whatever you want.

    <?php
        $filename = $_POST['filename'];
        $filedata = base64_decode($_POST['filedata']);
    ?>
    
    点赞 评论 复制链接分享

相关推荐