Jump to content

help me solve a file edit problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
111222

111222

    Newbie

  • Members
  • Pip
  • 5 posts
how can i make the php to open a file, search a specific sentance in it, replace it to another sentance, and then save the file automaticly without "777 command".

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
//To open/read file contents:
$myFile = "filename.ext";
$fh = fopen($myFile, 'r');
$data = fread($fh, 9999999);
fclose($fh);

//Search the data and replace a string:
$data = str_replace("STRING HERE", "REPLACE HERE", $data);

//To write to a file:
$myFile = "filename.ext";
$fh = fopen($myFile, 'a');
fwrite($fh, $data);
fclose($fh);


#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Here is better code:


//To open/read file contents:

$myFile = "filename.ext";

$fh = fopen($myFile, 'r+');

$data = fread($fh, 9999999);


//Search the data and replace a string:

$data = str_replace("STRING HERE", "REPLACE HERE", $data);


//To write to a file:

fwrite($fh, $data);

fclose($fh);  



#4
111222

111222

    Newbie

  • Members
  • Pip
  • 5 posts
i sayed witout 777 commend.
when i do your programing
i get permission dineded
i have 644 commend.

#5
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
That is an OS issue, not a PHP issue. You say 777 command you need to either be the user that owns the file or in the group that owns the directory or chmod the file to '777'.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I' pretty sure you can do this using the FTP wrapper along with some file system functions.

#7
111222

111222

    Newbie

  • Members
  • Pip
  • 5 posts
hah!? you dont helped me.
i want it to edit the file without commend it (the orginal commend is 644)
plez give me a script to edit the file with the orginal commend (644)

#8
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
The file system commands will still not help him if he doesn't own the file (doesn't have write permission).

#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Well if he doesn't own the file, it is impossible to write or execute that file. That's why permissions exist.

However he never said he didn't own the file. Assuming he uploaded the file, the FTP user does own the file and has mod 6 - meaning he can read (4) and (+) write (2). The php user only has a mod of 4 meaning he can only read. Which is why, using the file system commands with the php user will not work, but using the file system commands with the FTP user will...

#10
111222

111222

    Newbie

  • Members
  • Pip
  • 5 posts
i own the file!!! i just want to edit it without givin him a command.
how can i do that?

#11
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
As I said before:

Sidewinder said:

I' pretty sure you can do this using the FTP wrapper along with some file system functions.

A combination of the code snippets found in the PHP manual [click the links I provided] and what Jordan posted above should be all you need.

#12
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
You are either going to have to chmod it manually or chmod it by code as Sidewinder has suggested. Once you have done that then use Jordan's code. If you chmod it by code you can always chmod it back to what it was at the end.