Results 1 to 8 of 8

Thread: How to use function file_get_contents?

  1. #1
    Join Date
    Jan 2010
    Posts
    311

    How to use function file_get_contents?

    Hello,
    Could you tell me how one uses the function file_get_contents. According to the documentation, this function reads an entire file into a string, it means that all character strings in the file are stored in a variable? I find it hard to understand. Moreover when one reads an entire file, we must first make an fopen on the file or not? How do I use fopen file_get_contents? Any more discussion on this topic will interest.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: How to use function file_get_contents?

    See if this code can help you

    Code:
    <Php
    $ fl = fopen(fl.php, w +), / / was opening my php fl
    $ test = file_get_contents($ fl); / / I read all the character of the fl
    echo $ test; / / I display all the contents of the fl
    fclose($ fl);
    ?>

  3. #3
    Join Date
    Mar 2010
    Posts
    338

    Re: How to use function file_get_contents?

    Could you tell me why my preg_replace does not work.
    PHP - Selecting
    $ temp = file_get_contents('exm.php');
    $ temp = preg_replace('# ^ $ # test', 'some', $ temp);
    echo $ temp;

    The file contains exm three lines:
    Other - Select
    Code:
    <p> Hello World </ p> 
    <p> Good </ p> 
    <p> Bye </ p>
    Could you please explain me this?

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: How to use function file_get_contents?

    Here is the code you are looking for
    Code:
         $ temp = file_get_contents('temp.php');
                    $ temp = preg_replace('# ^ $ # blah', 'kikou', $ temp),
                    $ ctr=stripslashes($ temp) ; 
                    fwrite($ fl, $ ctr); 
                    fclose($ fl) ; / / We close the fl * /
                    echo 'Editing done successfully' ;
    If you have error using this function see here
    PHP error loading file_get_contents

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: How to use function file_get_contents?

    By running the above code I get an error, here it is
    Warning: fputs (): Supplied argument Is Not a valid stream resource in c: \ program files \ easyphp1-8 \ www \ test \ folder \ temp.php on line 28

    Warning: fclose (): Supplied argument Is Not a valid stream resource in c: \ program files \ easyphp1-8 \ www \ test \ folder \ temp.php on line 29
    Change completed successfully

  6. #6
    Join Date
    Mar 2010
    Posts
    372

    Re: How to use function file_get_contents?

    file_get_contents returns only the contents of a file, this is not a resource. Set (I think)
    (And it is slower than fopen and other )

  7. #7
    janysek Guest

    Re: How to use function file_get_contents?

    Function reads the entire file contents into a string. This is the best way to read the contents of a file. This method is preferred by developers because it uses less memory because of the use of memory mapping techniques.

  8. #8
    Join Date
    Mar 2010
    Posts
    200

    Re: How to use function file_get_contents?

    This is not a pointer to a file open .
    Code:
    $ Handle = fopen ('doc.txt', 'r');
    Is not equal to:
    Code:
    $ Content = file_get_contents ('doc.txt');
    file_get_contents () returns the CONTENT of the file, fopen () returns him something that serves to open the file. Basically, when you write file_get_contents () is as if you did fopen (), fread () (or fgets ()), fclose. You understand ?

Similar Threads

  1. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  2. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  3. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  4. PHP error loading file_get_contents
    By Soft Pack in forum Software Development
    Replies: 2
    Last Post: 21-05-2009, 02:47 PM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,571,789.38613 seconds with 17 queries