php fwrite()不';无法将字符串数据写入文件,为什么?

php fwrite()不';无法将字符串数据写入文件,为什么?,php,fopen,fwrite,Php,Fopen,Fwrite,我正在尝试将一个相当大的数据块写入一个文件,该文件是通过php中的fopen()打开的。我使用的协议包装器是ftp,因此该文件对运行php代码的服务器是远程的。我要写入的文件位于Windows服务器上 我验证了文件确实是由我的php代码创建的,但问题是文件中的数据不是不存在(0KB),就是写入文件的操作过早停止。不知道为什么会这样 以下是我用于处理该操作的代码: $file_handle = fopen($node['ftp'].$path_to_lut, "wb", 0, $node['ftp

我正在尝试将一个相当大的数据块写入一个文件,该文件是通过php中的fopen()打开的。我使用的协议包装器是ftp,因此该文件对运行php代码的服务器是远程的。我要写入的文件位于Windows服务器上

我验证了文件确实是由我的php代码创建的,但问题是文件中的数据不是不存在(0KB),就是写入文件的操作过早停止。不知道为什么会这样

以下是我用于处理该操作的代码:

$file_handle = fopen($node['ftp'].$path_to_lut, "wb", 0, $node['ftp_context']);
include_once($file);

if ($file_handle)
{
     fwrite($file_handle, $string);  //$string is inside included $file
     fclose($file_handle);
} else {
     die('There was a problem opening the file.');
}
当我在本地机器上托管它时,该代码工作正常,但当我将其上载到我的webhost(Rackspace Cloud)时,它失败了。这让我相信这是一个和Rackspace服务器的配置有关的问题,但我想知道我是否可以对我的php代码做些什么,使其更加健壮

有没有办法确保fwrite将字符串写入远程机器

谢谢

好的,我更改了写入文件的代码,如下所示:

if ($file_handle)
{
    if ($bytesWritten = fwrite($file_handle, $string) ) {
        echo "There were " . $bytesWritten . " bytes written to the text file.";
    }

    if (!fflush($file_handle)) {
        die("There was a problem outputting all the data to the text file.");
    }

    if (!fclose($file_handle)) { 
        die("There was a problem closing the text file."); 
    }

} else {

    die("No file to write data to.  Sorry.");

}
奇怪的是,echo语句显示以下内容:

有10330个字节写入文本文件

然而,当我通过FTP验证文本文件大小时,它显示为0K,并且文件中的数据实际上被截断了。我无法想象它与FTP服务器本身有关,因为如果PHP托管在Rackspace Cloud上的机器之外的机器上,它就可以工作

**更新** 我和一位Rackspace云服务代表谈过,他提到,如果您要从他们的服务器使用ftp,他们需要被动ftp。我将远程服务器设置为处理被动ftp连接,并已验证被动ftp现在可以通过OSX传输ftp客户端在远程服务器上工作。我补充说:

ftp_pasv($file_handle, true);
就在fopen()语句之后,但是我从PHP得到一个错误,说我没有为ftp_pasv()提供有效的资源。如何确保与PHP建立的ftp站点的连接是PASV且不处于活动状态,并且仍然使用fwrite()?顺便说一句,我注意到Windows机器报告我的PHP代码编写的文件在磁盘上有4096字节。它永远不会超过这个数量。这导致我将输出缓冲php值更改为65536只是为了解决问题,但这也没有解决问题

**更新部分DUEX**

解决Rackspace Cloud Sites产品上我的虚拟服务器上的问题太难了,因为它们没有提供足够的管理权限。我在Rackspace的云服务器产品上创建了一个非常小的云服务器,并对所有内容进行了配置,直到我仍然看到与fwrite()相同的错误。为了确保可以将文件从该服务器写入远程服务器,我在云服务器上的bashshell中使用了基本的ftp命令。它工作得很好。因此,我假设fwrite()的php实现中存在一个bug,这可能是由于某种类型的数据限制问题造成的。当我从本地环境向远程服务器写入数据时,与Rackspace云服务器上提供的数据相比,本地环境的上升速度较慢,因此工作正常。有没有办法有效地降低写入速度?只是问:)

**更新第三部分*

因此,我采纳了@a sad dude的建议,实现了一个函数,可以帮助尝试写入新文件并通过ftp发送整个文件:

function writeFileAndFTP($filename=null, $data=null, $node=null, $local_path=null, $remote_path=null)
{

    //  !Determin the path and the file to upload from the webserver
    $file = $local_path.'/'.$filename;


    //  !Open a new file to write to on the local machine
    if (!($file_handle = fopen($file, "wb", 0))) { 
        die("There was a problem opening ".$file." for writing!");
    }


    //  !Write the file to local disk
    if ($bytesWritten = fwrite($file_handle, $data) ) {
        //echo "There were " . $bytesWritten . " bytes written to " . $file;
    }

    //  !Close the file from writing
    if (!fclose($file_handle)) {
        die("There was a problem closing " . $file);
    }

    //  !Create connection to remote FTP server
    $ftp_cxn = ftp_connect($node['addr'], $node['ftp_port']) or die("Couldn't connect to the ftp server.");

    //  !Login to the remote server
    ftp_login($ftp_cxn, $node['user'], getPwd($node['ID'])) or die("Couldn't login to the ftp server.");

    //  !Set PASV or ACTIVE FTP
    ftp_pasv($ftp_cxn, true);


    //  !Upload the file
    if (!ftp_put($ftp_cxn, $remote_path.'/'.$filename, $file, FTP_ASCII)) {
        die("There was an issue ftp'ing the file to ".$node['addr'].$remote_path);  
    }

    //  !Close the ftp connection
    ftp_close($ftp_cxn);

}

在某些平台上,可以一次性写入的字符串
fwrite
的长度受到限制(这就是它返回写入字节数的原因)。您可以尝试在循环中运行它,但更好的方法是简单地使用
file\u put\u contents
,这可以保证整个字符串都将被写入


包含一次($file)-代码在哪里?看起来如下:
$string=“大约20行长的字符串”什么是
var_转储($node['ftp_context'])输出?(删除用户名/密码)。类型为(流上下文)的资源(32)同样,代码
var_dump(流获取元数据($file_handle))输出:
array(10){[“wrapper_data”]=>NULL[“wrapper_type”]=>string(3)“ftp”[“stream_type”]=>string(14)“tcp_socket/ssl”[“mode”]=>string(2)“r+”[“unread_bytes”]=>int(0)[“seakable”]=>bool(false)[“uri”]=>string(119)”ftp://user:pass@mydomain.com:21/vars.txt“[“超时”]=>bool(假)[“阻塞”]=>bool(真)[“eof”]=>bool(false)}
很有趣,谢谢你的指针。出于好奇,我是否可以在.htaccess文件中设置apache指令来更改fwrite()的数据限制?值得怀疑)另外,如果你使用ftp,我想更多的是关于网络的。文件内容有用吗?不,没用。文件被创建,但没有写入数据。通过Apache,您可以更改PHPINI指令(假设PHP模块已打开)。但这不是一个好的做法。但是,您必须检查PHP是否有这种选项,您可能需要检查服务器上是否启用了PHP模块,然后再试一次。或者一些PHP库。