gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:10 25 4
gpt4 key购买 nike

我尝试使用 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).

最佳答案

  • 无法写入“123.rar”(权限被拒绝)。 表示您的 PHP(mod_php 以 Apache 用户身份运行?)没有权限将文件写入本地服务器。您需要指定(或创建,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 文件,不是吗?)。

关于php - 从远程服务器下载文件,下载完成后删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com