duanhong1985 2013-05-15 09:58
浏览 60

从php调用时,无法使用带有身份验证的wget下载文件

I've created a small remote download script which downloads remote files to my server using wget. Now here's how it works: I have a html form, which the user can enter the URL, destination file name, and optionally username and password to authenticate, in case needed. The form calls a php script, using AJAX, and the php script passes the information to a shell script. Now the problem is, the shell script works flawlessly on my local linux machine, but on my server, it doesn't work with authentication(without authentication, it works just fine.)

#!/bin/bash

if test $# -eq 2
then
    wget -O "$2" $1
elif test $# -eq 4
then
    wget -O "$2" --http-user="$3" --http-password="$4" $1
else
    wget $1
fi

it might be worth mentioning that, I own a shared server.

Thanks

EDIT: Is it possible that the version of wget installed on the server, doesn't support http authentications???

EDIT 2: I piped the output of wget to a file like this:

wget -O "$2" --http-user="$3" --http-password="$4" $1 | echo >> output

but the output is empty, i.e. no messages is being printed from wget!!! And I did this too, to check if the credentials passed is ok:

echo "$3 | $4" >> credentials

And it was ok.

And this is the php code which runs the shell script:

if(isset($_POST["startdownload"]))
{
    list($url, $dest, $user, $pass) = explode("|", urldecode($_POST["data"]));
    $filelen = file_len($url);
    $freespace = getRemainingSpace();
    //die( "( Free: $freespace, File: $filelen )" );
    if($filelen > $freespace - 10000)
    {
        $result = json_encode(array("error" => true, 
                        "message" => "There is not enough space to download this file. ( Free: $freespace, File: $filelen )"));
        die($result);
    }
    else
    {
            if($user != "NULL" && $pass != "NULL")
            {
                execInBackground("../dl.sh '{$url}' '../{$dest}' '{$user}' '{$pass}'| at now");
            }
            else
            {
        execInBackground("../dl.sh '{$url}' '../{$dest}' | at now");    
            }
            $result = json_encode(array("error" => false, "message" => "Starting download..."));
            die($result);
    }
}
function execInBackground($cmd) 
{
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r")); 
    }
    else {
        exec($cmd . " > /dev/null &");  
    }
} 
  • 写回答

1条回答 默认 最新

  • douzhi4991 2013-05-15 11:38
    关注

    Well, running this script via system/exec/shell_exec or any others, probably going to "block" the rest of the PHP script anyway. But as an answer, check out this: http://www.zarafa.com/wiki/index.php/Using_wget_and_HTTP_authentication_for_supported_downloads

    wget -O "$2" --user="$3" --ask-password="$4" $1
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图