gpt4 book ai didi

php - 如何在 PHP 中通过 ftp 上传文件?

转载 作者:行者123 更新时间:2023-12-01 02:50:42 26 4
gpt4 key购买 nike

我有一个带有 html 浏览类型和提交按钮的表单。我使用浏览按钮选择了一个文件并提交了表单。在提交表单时调用以下代码。

$conn_id="myid";
$conn_id = ftp_connect ( 'server' );
$ftp_user_name="username";
$ftp_user_pass="password";

// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );

// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;

} else {
echo "Connected to for user $ftp_user_name" ;
}

// upload the file
$upload = ftp_put( $conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY );

// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
} else {
echo "Successfully Uploaded." ;
}

但它会产生以下警告:

Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!

但是当我在上面的代码中对源路径进行硬编码时,它会将文件上传到服务器上:

$upload = ftp_put( $conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY );

最佳答案

试试这个:

$file = 'somefile.txt';
$remote_file = '/public_html/dir/dir2/dir3/somefile.txt';
$ftp_server = "yourdomain.in";
$ftp_user_name = "ftpusername";
$ftp_user_pass = "ftppassword";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

您的文件将上传到您域的 /public_html/dir/dir2/dir3/ 目录中。
注意:测试此功能时切勿上传索引文件。
来源:PHP.net

关于php - 如何在 PHP 中通过 ftp 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3502706/

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