1

I have a PHP program which reads out and writes to a file which is on an FTP server, the first time writing goes fine, but when I try to write a second time I got 2 different errors:

Warning: file_put_contents(ftp://...@Directory/map/Sources/data.txt): failed to open stream: FTP server reports 550 End in C:\Apache24\htdocs\Directory\Sources\form.php on line 42

and the second:

Warning: file_put_contents(ftp://...@Directory/map/Sources/data.txt): failed to open stream: Failed to set up data channel: No connection could be made because the target machine actively refused it. in C:\Apache24\htdocs\Directory\Sources\form.php on line 42

And then it just repeats the second error.

I haven't tried anything yet, because I don't know where to start.

session_start();

$path = 'ftp://username:password@Directory/map/Sources/data.txt'; //FTP
//$path = 'C:\xampp\htdocs\NPS\Sources\data.txt'; //local path
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);

$text_file = fopen($path,'r'); //open file
$text_data = file_get_contents($path, filesize($path)); //read out file

//write old data along with new data
file_put_contents($path, $text_data . "text", 0, $stream_context);

//session stop
session_destroy();

I expect the code to write the data which I fill in beforehand with a form which I send, however it writes only once and then it just shows the errors.

DiceOfDoom
  • 25
  • 4
  • Does that error happen when the email is not correct, when it is correct or on both cases? – Maxime Launois Jul 15 '19 at 13:21
  • 1
    Why do you perform that email pattern check inside the while loop to begin with? – misorude Jul 15 '19 at 13:27
  • Do you get the same problem, if you remove the `FILE_APPEND`? – Martin Prikryl Jul 15 '19 at 13:30
  • @misorude I do not think that's relevant to the question. Though OP should have indeed posted [mcve]. What would eliminate the code that you refer to. – Martin Prikryl Jul 15 '19 at 13:31
  • The path should be full. Is Directory/map/Sources/data.txt correct? – Erwin Moller Jul 15 '19 at 13:31
  • @MartinPrikryl it might not be _directly_ relevant to the question, but the overall approach seems questionable to me, in how the code is written - hard to understand what the purpose is at a glance, this could be written way “cleaner” IMHO. – misorude Jul 15 '19 at 13:35
  • 1
    Possible duplicate of [Getting "FTP server reports 550 Could not get file size." when using FTP URL in fopen](https://stackoverflow.com/questions/43505357/getting-ftp-server-reports-550-could-not-get-file-size-when-using-ftp-url-in) – misorude Jul 15 '19 at 13:36
  • 1
    That duplicate seems like quite a good candidate to me. If your FTP server does not support the SIZE command, that should not be a problem when writing a _new_ file - but if you want to _append_ to one, the current size of the file probably needs to be known, so that would explain why it fails on the second attempt then. – misorude Jul 15 '19 at 13:38
  • @MaximeLaunois The error happens when it is correct, when the email is incorrect it doesn't read nor write out of the file, it just shows "wrong email" and sends the user back to the previous page. – DiceOfDoom Jul 15 '19 at 14:26
  • @MartinPrikryl Yes, I do get the error even if I remove the `FILE_APPEND`, I tried putting a 0 instead where the FILE_APPEND is supposed to be, but still got the same result – DiceOfDoom Jul 15 '19 at 14:27
  • OK, so start by posting [mcve]. – Martin Prikryl Jul 15 '19 at 14:31
  • It looks like IIS server. Is it? Do you have an access to server's log file? – Martin Prikryl Jul 16 '19 at 11:06
  • @MartinPrikryl It was a IIS Server, but recently we switched to Apache. Because when it still was a IIS server I would get a `500 - Internal Server Error` and the code wouldn't even work. – DiceOfDoom Jul 16 '19 at 11:43
  • Are you sure? `550 End` is IIS-style message. + What Apache? + Do you have the logs in any case? – Martin Prikryl Jul 16 '19 at 11:46

0 Answers0