【问题标题】:PHP FTP Error: 451 - Append/Restart not permittedPHP FTP 错误:451 - 不允许追加/重新启动
【发布时间】:2014-01-29 14:09:20
【问题描述】:

当尝试使用a 选项fopen 现有文件时,我收到此错误:

警告: fopen(ftp://...@sub.mysite.com/var/www/diversos/01_2014.txt) [function.fopen]:打开流失败:FTP 服务器报告 451 /var/www/diversos/01_2014.txt:不允许追加/重启,试试 再次在/www/html/prod/my_transfer_file.php 150

my_transfer_file.php - 第 150 行

fopen ('ftp://user:pass@sub.mysite.com/var/www/diversos/01_2014.txt', "a" );

是 FTP 还是代码问题?我该怎么做才能解决这个问题? 以前从未见过此错误。

【问题讨论】:

  • 我很确定我回答了你的问题。这不一定是配置问题。

标签: php file ftp


【解决方案1】:

这意味着另一端的 FTP 服务器不支持将数据附加到文件中。由于这是服务器级别的配置,除非您具有对服务器的管理访问权限来更改设置,否则您真的无能为力。

我唯一能建议的是下载完整文件,在本地附加,删除远程,然后上传附加文件。您可以使用PHP FTP library

来做到这一点
$ftp = ftp_connect('yourserver.com');
$local = 'localfile.txt';
$remote = 'remote.txt';
if(ftp_login($ftp, 'username', 'password')){
    ftp_get($ftp, $local, $remote);
    $file = fopen($local, 'a');
    fwrite($file, 'your data here');
    fclose($file);
    ftp_delete($ftp, $remote);
    ftp_put($ftp, $remote, $local, FTP_ASCII); // It's a text file so it will be ASCII
    ftp_close($ftp);
}

【讨论】:

  • 谢谢!我可以访问 FTP 服务器配置,我需要在服务器中运行什么命令才能允许这种Append/Reset
  • 我建议询问有关服务器配置的另一个问题,并将其链接到此处。
  • 这不是配置问题
  • 此外,并非所有服务器都安装了 FTP 扩展。除非需要高级客户端功能,否则流通常是更好的选择。
【解决方案2】:

在 fopen 中使用“a”选项时,我的服务器给了我同样的信息。 'a' 选项将文件指针放在文件末尾,这意味着任何写入都将添加数据而不是覆盖文件。检查它是否只使用'w'选项,例如

fopen ('ftp://user:pass@sub.mysite.com/var/www/diversos/01_2014.txt', "w" );

如果您需要预先添加,请先读取文件,然后将新内容添加到本地文件的末尾。

$file ="ftp://user:pass@domain.com/file.ext";
$stream  = fopen($file, 'r');

$contents = fread($stream, 1024);

// since your likely not just reading it for fun
$contents = do_something_to_contents($contents);

$opts = array('ftp' => array('overwrite' => true));
$context = stream_context_create($opts);

$stream  = fopen($file, 'w', false, $context);

fwrite($stream, $contents);

在我的服务器上,我不得不打开流两次,因为它不允许它以读/写模式打开(选项 'wr' 或 'w+')

您也可以尝试使用 file_get_contents 和 file_put_contents

// the file your trying to get
$file ="ftp://user:pass@domain.com/file.ext";

// get the file
$contents = file_get_contents($file);

// write
$opts = array('ftp' => array('overwrite' => true));
$context = stream_context_create($opts);
file_put_contents($file, $contents, NULL, $context);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多