php - download file from remote server and delete after complete downloading
Get the solution ↓↓↓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).
Answer
Solution:
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 havessh
access (but then you would justscp
the file in the first place, wouldn't you?).
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: filter_sanitize_string deprecated
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.