吧数据写入文件php,php fwrite()没有完成将字符串数据写入文件,为什么?

我正在尝试将大量数据写入通过PHP中的fopen()打开的文件.我正在使用的协议包装器是ftp,因此该文件远程运行PHP代码的服务器.我正在写的文件是在

Windows服务器上.

我确认该文件实际上是由我的PHP代码创建的,但问题是文件中的数据要么不存在(0KB),要么过早地写入文件.不知道为什么会这样.

这是我用来处理操作的代码:

$file_handle = fopen($node['ftp'].$path_to_lut,"wb",$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语句显示如下:

There were 10330 bytes written to the text file.

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

**更新**

我采访了一位Rackspace Cloud代表,他提到如果你要从他们的服务器上ftp,他们需要被动ftp.我设置远程服务器来处理被动ftp连接,并验证了被动ftp现在可以通过OSX Transmit ftp客户端在远程服务器上运行.我补充说:

ftp_pasv($file_handle,true);

在fopen()语句之后,但是我从PHP得到一个错误,说我没有为ftp_pasv()提供有效的资源.如何确保与PHP生成的ftp站点的连接是PASV而不是ACTIVE并且仍然使用fwrite()?顺便说一句,我注意到Windows机器报告我的PHP代码写的文件在磁盘上是4096字节.它永远不会超过这个数量.这导致我将output_buffering PHP值更改为65536只是为了进行故障排除,但这也没有解决问题. . .

**更新部分DUEX **

对Rackspace Cloud Sites产品上的虚拟服务器上的问题进行故障排除过于困难,因为它们没有提供足够的管理权限.我在Rackspace的Cloud Server产品上创建了一个非常小的云服务器,并将所有内容配置到我仍然看到与fwrite()相同的错误.为了确保我可以将该服务器中的文件写入远程服务器,我在云服务器上的bash shell中使用了基本的ftp命令.它工作正常.因此,我假设fwrite()的PHP实现中存在一个错误,并且可能是由于某种类型的数据限制问题.当我从本地环境写入远程服务器时,与Rackspace Cloud服务器上提供的速度相比,速度慢得多,它运行正常.有没有办法有效地降低写入的速度?请问’:)

**更新第III部*

所以,我从@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,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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值