codemania
codemania

Reputation: 1094

download file from remote server and delete after complete downloading

i try to download file from remote server by use 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).

Upvotes: 0

Views: 1690

Answers (1)

grebneke
grebneke

Reputation: 4494

  • 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. You would need to specify (or create, chmod 777) a directory where writing is permitted.

Let's assume your Apache can save to /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?).

Upvotes: 1

Related Questions