在 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);