0

I have a PHP code that connects to a FTP server, reads a file's content, makes a minor change, and than overrides the original file.

it looks like:

$stream_context = stream_context_create(array('ftp' => array('overwrite' => true)));

$file_content = file_get_contents($ftp_file); // this line works
$file_content = str_replace('some content', 'another content', $file_content); // this also..
file_put_contents($ftp_file, $file_content, 0, $stream_context);  // this one doesn't :/

the real issue is that the "file_put_contents" worked for a long time, but now it doesn't. what it does now is weird: it deletes the original file from the server.. also, if i'm changing it to something like:

file_put_contents($new_ftp_file, $file_content);

from what I know, it should create the new file and put the content in it, but it doesn't create it at all.

the hosting service i'm using has a PHP version change a few days ago. I don't remember the what the previous version was, but the current is: 5.2.17

thanks! :)

some changes

I found this piece: http://www.php.net/manual/en/function.file-put-contents.php#86864 doting the same as "file_put_contents" but with foen, fwrite and fclose (I send his example because of the results..). his functions is returning "false" if it couldn't to "fopen" the file, or the "bytes" if it succeeded. I got "false" :/ which means it couldn't even do the:

@fopen($filename, 'w');

although the "file_get_contents" with the same file address is working.


reading is working (but if you take the $filename and use it yourself on a client FTP - it works):

@fopen($filename, 'r');

the "open base_dir" for my hosting (which makes the action) is set to false, but the target hosting (which has the target-file) is set to be true.


I had an idea to save the new content on a new file, so I tried something like:

$f = @fopen($new_ftp_file, 'w'); //this one seems to work and connect
fwrite($f, $file_content); // this one seems to work either and returning the number of byes..
fclose($f);

the problem is that none of them really works. I logged in to the FTP address, using the same credentials that my script is using, and I haven't found the new file. It wasn't created at all. (as I remind you, "$new_ftp_file" is a path to a file that doesn't exists, so "w" mode on "fopen" should create it).

David Levy
  • 203
  • 2
  • 10

2 Answers2

1

file_put_contents do erase the file by default if it already exists. You have to ask him to don't. Look at here: http://www.php.net/manual/en/function.file-put-contents.php See: If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
Valentin BEAULE
  • 384
  • 1
  • 7
  • 26
  • I want it to replace the original file, by deleting it and put the new content. the problem is that it deletes the original but not add the new content. – David Levy Jun 17 '14 at 15:03
  • It could be because you lost some right on your server with the previous change. Be sure you can create/edit files in your directory. Also the file_put_content function works with php5. ;) – Valentin BEAULE Jun 17 '14 at 15:12
  • the $ftp_file is a combination of another ftp_hosting, using: ftp://[user]:[password]@[file_adress] - this used this same address to change the target file myself.. – David Levy Jun 17 '14 at 15:23
0

this is not the best solution (definitely), but I managed to figure something.. I used 2 different hosts on them same hosting service, both of the having "open base_dir = On" & "safe mode = Off".

it looks something like:

$ftpstring = "ftp://user:password@anotherhosteddomain.com";
$file = file_get_contents($ftpstring . 'index.html'); // this line works as expected. (yeah, the other hosting has this file);

and then, if you're trying to write something like:

$handler = fopen($ftpstring.'index.html', "w");

it wouldn't work, and tell you it cannot access on writing mode to an existing file. so if you're doing something like:

$newfile_handler = fopen($ftpstring.'index_new_version.html', "w");
fwrite($newfile, "1122");

so yeah - it works! but now is a tricky issue.. when i'm adding this line:

fclose($newfile_handler);

the new file is deleted from the hosting!!

I couldn't find any reason why "fclose" is deleting the file after it was create at "fopen" and written in at "fwrite".

so if you're not adding the "fclose" line - it works, but it doesn't close the connection, and also I have to actually delete the existing file before I can override it with a new content, which makes it silly.. although it works, I would really like someone to give me a better solution than mine.

David Levy
  • 203
  • 2
  • 10