ftp_put上传一个空文件

ftp_put上传一个空文件

问题描述:

I'm trying to upload a file via ftp_put to a windows server.

my code is as follows:

    $date           = date('ymd');
    $file_name      = $date.'.csv';
    $file_location  = 'D:/inetpub/wwwroot/website.com/html/assets/'.$file_name;

//set up basic connection
$conn_id = ftp_connect(FTP_HOST, FTP_PORT);

// login with username and password
$login_result = ftp_login($conn_id, FTP_USER, FTP_PASS);

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!"; 
    exit;
}  else { 
    echo "Connected to FTP Server";
}

$upload = ftp_put($conn_id, $file_name, $file_location, FTP_ASCII);

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

// close the FTP stream 
ftp_close($conn_id); 

If I upload the file manually using filezilla, it works perfectly. If I use the code above, it creates an empty file.

我正在尝试通过ftp_put将文件上传到Windows服务器。 p> 我的代码如下: p>

  $ date = date('ymd'); 
 $ file_name = $ date。'。csv'; 
 $ file_location =  'D:/inetpub/wwwroot/website.com/html/assets /'.$ file_name; 
 
 //设置基本连接
 $ conn_id = ftp_connect(FTP_HOST,FTP_PORT); 
 
 //登录 用户名和密码
 $ login_result = ftp_login($ conn_id,FTP_USER,FTP_PASS); 
 
 //检查连接
if((!$ conn_id)||(!$ login_result)){
 echo“FTP连接 失败了!”;  
退出; 
}其他{
 echo“连接到FTP服务器”; 
} 
 
 $ upload = ftp_put($ conn_id,$ file_name,$ file_location,FTP_ASCII); 
 
 //检查 上传状态
if(!$ upload){
 echo“FTP上传失败!”;  
} else {
 echo“File Uploaded”; 
} 
 
 //关闭FTP流
ftp_close($ conn_id);  
  code>  pre> 
 
 

如果我使用filezilla手动上传文件,它可以完美地运行。 如果我使用上面的代码,它会创建一个空文件。 p> div>

It turns out that UKFast was blocking the connection and transfer. (They also require it to be Active Mode only).

Now they've unblocked it, it's working perfectly. (Before it seemed to just time out)

try using FTP_BINARY instead of FTP_ASCII like this.

$upload = ftp_put($conn_id, $file_name, $file_location, FTP_BINARY);

PHP ftp can be buggy but I have found that it pretty much works in binary transfer mode.

Try transferring the file with passive mode enabled:

Passive Mode

turn passive mode on

  ftp_pasv($conn_id, true);

thanks "Khan Muhammad" for your answer, when I added this part :

ftp_pasv($conn_id, true);

the file was uploaded perfectly.