Php fwrite () does not finish writing string data to a file, why?

I am trying to write a significant portion of the data to a file that opens through fopen () in php. The protocol wrapper that I use is ftp, so the file is removed from the server where the php code is running. The file I am writing is on a Windows server.

I confirmed that the file is actually being created by my PHP code, but the problem is that the data in the file is either non-existent (0 KB) or writing to the file stops prematurely. I donโ€™t know why this is so.

Here is the code that I use to process the operation:

$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.'); } 

This code works fine when I host it on my local machine, but when I upload it to my web host (Rackspace Cloud), it fails. This makes me think that this is a problem with my server configuration in Rackspace, but I want to know if there is anything that I can do for my php code to make it more reliable.

Any ideas to ensure that fwrite actually finishes writing the string to the remote machine?

Thanks!

Ok, I changed the code that is written to the file as follows:

 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."); } 

It is strange that the echo statement shows the following:

10330 bytes are written to the text file.

And yet, when I check the size of the text file via FTP, it shows that it is 0 K, and the data inside the file is, in fact, truncated. I canโ€™t imagine that this is due to the FTP server itself, because it works if PHP is hosted on a machine different from the one on the Rackspace Cloud.

** UPDATE ** I spoke with a Rackspace Cloud representative who mentioned that they need passive ftp if you are going to ftp from your servers. I configure a remote server to handle passive ftp connections and verify that passive ftp now works on the remote server through the OSX Transmit ftp client. I added:

 ftp_pasv($file_handle, true); 

Right after the fopen () statement, but I get an error from PHP saying that I did not provide a valid ftp_pasv () resource. How can I make sure that the connection to the ftp site created by PHP is PASV and not ACTIVE and still uses fwrite ()? By the way, I noticed that the Windows machine reports that the file written by my PHP code is 4096 bytes on disk. He never goes beyond that amount. This led me to change the php value of output_buffering to 65536 just for troubleshooting purposes, but it also didn't fix the problem.,.

** UPDATE PART DUEX **

Troubleshooting on my virtual server on Rackspace Cloud Sites turned out to be too complicated because they do not offer enough administrator rights. I created a very small cloud server on the Rackspace Cloud Server product and configured everything to such an extent that I still see the same error with fwrite (). To make sure that I can write a file from this server to a remote server, I used the basic ftp commands in my bash shell on the cloud server. It worked fine. So, I assume that there is an error in the PHP implementation of fwrite (), and that this is probably due to the data limitation problem. When I write to a remote server from my local environment, which has a slower speed than what is offered on the Rackspace Cloud server, it works fine. Is there a way to effectively reduce the write speed? Just ask :)

** UPDATE PART III *

So, I accepted the offer from @a sad dude and implemented a function that could help someone try to write to a new file and send it completely via 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); } 
+6
source share
1 answer

The length of the fwrite string can be written at one time on some platforms (therefore, it returns the number of bytes written). You can try to run it in a loop, but itโ€™s best to just use file_put_contents , which ensures that the entire line is written.

http://www.php.net/manual/en/function.file-put-contents.php

+4
source

Source: https://habr.com/ru/post/909497/


All Articles