【问题标题】:PHP fwrite doesn't appear to be writing or ftp_fput is messing up?PHP fwrite 似乎没有写入或 ftp_fput 搞砸了?
【发布时间】:2013-12-09 21:44:52
【问题描述】:

我有以下代码

<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);

$conn = ftp_connect("ftp.MYSITE.com") or die("Failed to connect to FTP server");
ftp_login($conn, "USER", "PASS");
$my_file = 'test.txt';
$file_handler = fopen("test.txt", 'w');
fwrite($file_handler, "A second line");
ftp_fput($conn, "/nick/test.txt", $file_handler, FTP_ASCII);
fclose($file_handler);
ftp_close($conn);
?>

当我运行它时,文件 test.txt 出现在正确的文件夹中,但是 test.txt 文件是空的。为什么会这样,我该如何解决?

【问题讨论】:

  • 你试过ftp_fput($conn, "test.txt", $file_handler, FTP_ASCII);吗?由于您直接在$file_handler = fopen("test.txt", 'w'); 中访问它,因此还要检查文件权限。
  • 是的,做到了,您能解释一下发生了什么变化以及为什么它可以解决问题吗?
  • ftp?这是 2013 年,而不是第 70 年!
  • 因为文件写入通常不使用绝对路径,而是直接访问时使用相对路径。很高兴看到它对你有用,干杯。
  • @arkascha 你有什么推荐的? SSH?

标签: php file-upload file-io ftp


【解决方案1】:

只是为了让我们可以正确地结束问题(而不是将其放在未回答的类别中):

删除/nick/

ftp_fput($conn, "/nick/test.txt", $file_handler, FTP_ASCII);

您使用的是绝对路径而不是相对路径。

代码:

<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);

$conn = ftp_connect("ftp.MYSITE.com") or die("Failed to connect to FTP server");
ftp_login($conn, "USER", "PASS");
$my_file = 'test.txt';
$file_handler = fopen("test.txt", 'w');
fwrite($file_handler, "A second line");
ftp_fput($conn, "test.txt", $file_handler, FTP_ASCII);
fclose($file_handler);
ftp_close($conn);
?>

另外,如果您不需要通过 FTP 写入文件,您可以删除所有与 FTP 相关的代码。

没有它也会运行。

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2011-05-30
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多