简体   繁体   English

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

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

i try to download file from remote server by use wget. 我尝试使用wget从远程服务器下载文件。 and i want to delete remote server file after complete downloading. 我想在完全下载后删除远程服务器文件。

here is my code for download file. 这是我的下载文件代码。

<?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';   

}

?>

but this is also give error like below: 但这也给如下错误:

--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). means that your PHP (mod_php running as Apache user?) doesn't have permissions to write the file to your local server. 表示您的PHP(以Apache用户身份运行的mod_php?)没有权限将该文件写入本地服务器。 You would need to specify (or create, chmod 777 ) a directory where writing is permitted. 您需要指定(或创建chmod 777 )允许写入的目录。

Let's assume your Apache can save to /tmp 假设您的Apache可以保存到/tmp

$local_dir = '/tmp';
shell_exec("wget -P $local_dir http://192.168.X.X/123/123.rar");
  • Deleting the remote file needs permissions and access to the remote server. 删除远程文件需要权限和对远程服务器的访问权限。 Several options exist depending on your setup: ssh remote-server "rm /path/to/123/123.rar" if you have ssh access (but then you would just scp the file in the first place, wouldn't you?). 有几个选项存在根据您的设置: 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