繁体   English   中英

从远程服务器下载文件,下载完成后删除

[英]download file from remote server and delete after complete downloading

我尝试使用wget从远程服务器下载文件。 我想在完全下载后删除远程服务器文件。

这是我的下载文件代码。

<?php
function remoteFileExists($url) {
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_NOBODY, true);

    $result = curl_exec($curl);

    $ret = false;

    if ($result !== false) {
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

        if ($statusCode == 200) {
            $ret = true;   
        }
    }

    curl_close($curl);

    return $ret;
}


$exists = remoteFileExists('http://192.168.X.X/123/123.rar');
if ($exists) {

    shell_exec('wget  http://192.168.X.X/123/123.rar');
    echo"file downloaded";

} else {
    echo 'file does not exist';   

}

?>

但这也给如下错误:

--2014-01-28 11:17:38--  http://192.168.X.X/123/123.rar
Connecting to 192.168.X.X:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 345 [text/plain]
123.rar: Permission denied

Cannot write to `123.rar' (Permission denied).
  • Cannot write to '123.rar' (Permission denied). 表示您的PHP(以Apache用户身份运行的mod_php?)没有权限将该文件写入本地服务器。 您需要指定(或创建chmod 777 )允许写入的目录。

假设您的Apache可以保存到/tmp

$local_dir = '/tmp';
shell_exec("wget -P $local_dir http://192.168.X.X/123/123.rar");
  • 删除远程文件需要权限和对远程服务器的访问权限。 有几个选项存在根据您的设置: ssh remote-server "rm /path/to/123/123.rar"如果你有ssh访问(但你只想scp摆在首位的文件,不是吗?) 。

暂无
暂无

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

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