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语句显示如下:
然而,当我通过FTP验证文本文件大小时,它显示为0K,文件中的数据实际上是截断的.我无法想象它与FTP服务器本身有关,因为如果PHP托管在Rackspace Cloud上的机器之外,它就可以工作. **更新** 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); }
字符串fwrite可以一次写入的长度在某些平台上是有限的(这就是为什么它返回写入的字节数).您可以尝试在循环中运行它,但更好的想法是简单地使用file_put_contents,这可以保证将写入整个字符串.
http://www.php.net/manual/en/function.file-put-contents.php (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |