将带有消息的文本文件写入 FTP
Writte text file with message to FTP
我想在 FTP 服务器上创建一个文件夹并在该文件夹内创建一个包含给定消息的文件。
以下是我的尝试:
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
fwrite($myfile, $txt);
rewind($myfile);
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>
这将创建具有给定名称的文件夹,并将一个 message.txt 文件放入该文件夹,但该文件始终为空。如何将我的消息添加到文件中?
您似乎是以读取模式打开文件('php://temp', 'r+')
试试 fopen ('php://temp', 'w +')
我最喜欢使用 shell_exec
写入本地文件。我的解决方案如下所示。
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
// Here is the good stuff with shell_exec.
$myfilelocation = '~/myfile.txt';
shell_exec( "cat $txt >> $myfilelocation" );
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>
我想在 FTP 服务器上创建一个文件夹并在该文件夹内创建一个包含给定消息的文件。 以下是我的尝试:
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
fwrite($myfile, $txt);
rewind($myfile);
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>
这将创建具有给定名称的文件夹,并将一个 message.txt 文件放入该文件夹,但该文件始终为空。如何将我的消息添加到文件中?
您似乎是以读取模式打开文件('php://temp', 'r+')
试试 fopen ('php://temp', 'w +')
我最喜欢使用 shell_exec
写入本地文件。我的解决方案如下所示。
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
// Here is the good stuff with shell_exec.
$myfilelocation = '~/myfile.txt';
shell_exec( "cat $txt >> $myfilelocation" );
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>