4 Replies - 1711 Views - Last Post: 06 September 2012 - 02:02 AM Rate Topic: -----

#1 ktalebian  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 08-April 08

How to read PHP Files

Posted 01 October 2009 - 02:52 AM

Hi there,

Is there a way to actually open up the content of another PHP file, read it, edit it and save it again?
I used this command:

$file = "/file.php";
$data = fopen($file,"r");
echo "<textare>".$data."</textarea>";



but in the textarea it says: Resource id #2

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How to read PHP Files

#2 trizz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-October 08

Re: How to read PHP Files

Posted 01 October 2009 - 03:06 AM

Now you've only opened a file, you've to read it into a var:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;


(source: http://www.tizag.com...T/fileread.php)

More on writing to a file can be found here: http://www.tizag.com...T/filewrite.php
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3002
  • View blog
  • Posts: 10,398
  • Joined: 08-August 08

Re: How to read PHP Files

Posted 01 October 2009 - 05:26 AM

While it is possible to do this as php files are just text, I've yet to find a good reason to do it. There is very likely a better way to accomplish what ever your larger goal is.
Was This Post Helpful? 0
  • +
  • -

#4 Melva1969  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-September 12

Re: How to read PHP Files

Posted 05 September 2012 - 08:39 PM

Regards for sharing the information with us on www.dreamincode.net.
Was This Post Helpful? 0
  • +
  • -

#5 Trend-Bgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 27-August 12

Re: How to read PHP Files

Posted 06 September 2012 - 02:02 AM

Hi,

You should use the file_get_contents() function, which is recommended as a best practice for this purpose.

Be careful though, because I see you want to display the content of your file in a textarea. You should think about what you will put inside this textarea: if your PHP file also contains HTML, it might interfere (for example if the php file contains </textarea>, your initial textarea will be closed by the contents of the PHP file). You should escape the contents of the PHP files with the htmlspecialchars() function, and then unescaping the content of the returned form with htmlspecialchars_decode().

Regards,

Hi,

You should use the file_get_contents() function, which is recommended as a best practice for this purpose.

Be careful though, because I see you want to display the content of your file in a textarea. You should think about what you will put inside this textarea: if your PHP file also contains HTML, it might interfere (for example if the php file contains </textarea>, your initial textarea will be closed by the contents of the PHP file). You should escape the contents of the PHP files with the htmlspecialchars() function, and then unescape the content of the returned form with htmlspecialchars_decode().

Regards,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1