简体   繁体   English

PHP ftp上传错误

[英]PHP ftp upload error

I'm encountering strange behavior with a simple PHP script that uploads a file to a ftp server. 我在使用简单的PHP脚本将文件上传到ftp服务器时遇到奇怪的行为。

I've created the following stripped down version of my code which produces the same error: 我创建了以下精简版代码,产生了相同的错误:

<?php

error_reporting(E_ALL);
echo phpversion();

$ftpUrl = "mydomain";
$ftpUserName = "myuser";
$ftpPassword = 'mypass';    

$fileContents = "test"; 
file_put_contents('text.txt', $fileContents);


//open ftp connection
$conn_id = ftp_connect($ftpUrl, 1030);

 if(!$conn_id)
    die('error while connecting to ftp');

$login_result = ftp_login($conn_id, $ftpUserName, $ftpPassword);

 if (!$login_result) 
    die('ftp login failed!');

//switch to passive mode
ftp_pasv($conn_id, true);

 //upload file to ftp
try
{
    $fileToUpload = fopen('text.txt', 'r');

    if($fileToUpload == false)
        die('can\'t open file to send!');

    $upload = ftp_fput($conn_id, 'testfile.txt', $fileToUpload, FTP_ASCII);
}
catch(Exception $e)
{
    fclose($fileToUpload);
    ftp_close($conn_id);
    die('Error while uploading to ftp');            
}

fclose($fileToUpload);
ftp_close($conn_id);

if (!$upload) 
    die('ftp upload failed');

?> ?>

On the client's ftp server I get the following error when running the script. 在客户端的ftp服务器上,运行脚本时出现以下错误。 (the error occurs when trying to upload the file, setting up the connection an logging in works) (尝试上传文件,建立连接并进行登录时会发生错误)

Warning: ftp_fput() [function.ftp-fput]: php_connect_nonb() failed: Operation now in progress (115) in {path} on line 38

Warning: ftp_fput() [function.ftp-fput]: Type set to A in {path} on line 38
ftp upload failed
  • Uploading the same file with filezilla to this ftp server works fine. 使用filezilla将同一文件上传到此ftp服务器可以正常工作。
  • The file only contains a few lines of text. 该文件仅包含几行文本。
  • When running this script on a different server, it works fine. 在其他服务器上运行此脚本时,它可以正常工作。

The server uses php version 5.3.28 服务器使用php版本5.3.28

I've been struggling with this error for hours now and it is driving me crazy. 我已经为这个错误苦苦挣扎了几个小时,这让我发疯。 Any clues? 有什么线索吗?

Ftp_put() takes a file name as a parameter, you are passing a file handle Ftp_put()将文件作为参数,您正在传递文件句柄

Try 尝试

ftp_put($conn_id,'testfile.txt', 'test.txt', FTP_ASCII);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM