【问题标题】:Create and upload a new file via ftp in php [duplicate]在php中通过ftp创建和上传新文件[重复]
【发布时间】:2021-12-13 13:20:58
【问题描述】:

是否可以在 ftp 中上传新文件而不是重写旧文件?我需要用于创建新文件并通过 ftp 将其上传到服务器的代码。如果我尝试通过ftp_put() 上传它,它不起作用。

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
    $ftp = ftp_connect("web");
ftp_login($ftp, "user", "pass");
echo ftp_put($ftp, "Tulassi/tulasi_test", $objPHPExcel, FTP_BINARY);
ftp_close($ftp);
    exit;

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 我需要通过 ftp 上传一个新文件到服务器(不是覆盖)
  • 那么,究竟是什么阻止了你这样做呢? “不起作用”不是错误消息或明确的问题陈述。只要您正在编写的文件的文件名在 FTP 服务器(同一文件夹)中尚不存在,它就不应覆盖其他任何内容。
  • 我不知道这是不是正确的代码,你可以分享创建和上传文件到 ftp 的代码
  • 拥有一个用于创建文件并将其上传到 FTP 的代码。如果它不起作用,并且您需要我们的帮助,您必须向我们描述您的问题。作为一个疯狂的猜测,我会把你指向PHP ftp_put fails

标签: php server ftp


【解决方案1】:
<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = '';
$destination_file = "new.xls";
$source_file = $objWriter; 

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

// 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!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
     
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}

$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII); 

if (!$upload) { 
echo "FTP upload has failed!112";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}  
// close the FTP stream 
ftp_close($conn_id);
?>

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 这是什么?这是一个解决方案吗?还是什么?
猜你喜欢
  • 2011-03-30
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2013-03-24
相关资源
最近更新 更多