SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help me solve a file edit problem

    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. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hey 31. It's untested, but should work:
    PHP Code:
    <?php
    $searchstring 
    "somestring";
    $replacestring "replacement";
    $file "somefile.txt";
    $contents file_get_contents($file);
    $contents str_replace($searchstring$replacestring$contents);
    file_put_contents($file$contents);
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fatal error: Call to undefined function: file_put_contents() in /home/bublik/public_html/etc.php on line 21

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What PHP version are you using?

    If it's anything under 5, I would highly recommend upgrading. If your host doesn't allow it, they're a bad host. (PHP6 is nearly out - php4 is nearly deprecated. I, for one, think it's a bit late and PHP4 has been draining the PHP world of it's full reusable-coding capabilities).

    file_put_contents() was put in PHP5, I think.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i have php4....
    how can i install php5 on my server?

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you on a shared server or a dedicated server?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dedicated server... but im a resller...

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think the best option is to ask the host for the best way for upgrading to the latest version of PHP5. No doubt they have that asked alot.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i cant its not so easy....
    maby you can help me with my problem...
    i have a resller and im trying to do a header (google ads) to all the files.
    like this:
    ads
    $file

    how can i do it?

    if there is a way to edit the file and add to him a header. without 777 commend its will be very helpfull.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    some one? edit file in php4 without 777 commend...

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use the same code as I posted before, but put this at the top:
    PHP Code:
    <?php
    if (!function_exists('file_put_contents')) {
        function 
    file_put_contents($filename$data$respect_lock true)
        {
            
    // Open the file for writing
            
    $fh = @fopen($filename'w');
            if (
    $fh === false) {
                return 
    false;
            }

            
    // Check to see if we want to make sure the file is locked before we write to it
            
    if ($respect_lock === true && !flock($fhLOCK_EX)) {
                
    fclose($fh);
                return 
    false;
            }

            
    // Convert the data to an acceptable string format
            
    if (is_array($data)) {
                
    $data implode(''$data);
            } else {
                
    $data = (string) $data;
            }

            
    // Write the data to the file and close it
            
    $bytes fwrite($fh$data);

            
    // This will implicitly unlock the file if it's locked
            
    fclose($fh);

            return 
    $bytes;
        }
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did this but somting is wrong... i cant find it. can you fix it for me?
    $fi="conf_global.php";
    if(file_exists($fi))
    {
    $file = $_SERVER["PATH_TRANSLATED"];
    if (!function_exists('file_put_contents')) {

    function file_put_contents($file, $data, $respect_lock = true)

    {

    // Open the file for writing

    $fh = @fopen($file, 'w');

    if ($fh === false) {

    return false;

    }



    // Check to see if we want to make sure the file is locked before we write to it

    if ($respect_lock === true && !flock($fh, LOCK_EX)) {

    fclose($fh);

    return false;

    }



    // Convert the data to an acceptable string format


    if (is_array($data)) {

    $data = implode('', $data);

    } else {

    $data = (string) $data;

    }



    // Write the data to the file and close it

    $bytes = fwrite($fh, $data);



    // This will implicitly unlock the file if it's locked

    fclose($fh);



    return $bytes;

    }

    }
    else {}
    $searchstring = 'global $ibforums;
    return <<<EOF';

    $replacestring = 'global $ibforums;
    return <<<EOFF
    <center>
    <script type="text/javascript"><!--
    google_ad_client = "pub-3806911217709370";
    google_ad_width = 728;
    google_ad_height = 90;
    google_ad_format = "728x90_as";
    google_ad_type = "text_image";
    google_ad_channel = "";
    google_color_border = "000000";
    google_color_bg = "FFFF66";
    google_color_link = "0066CC";
    google_color_text = "000000";
    google_color_url = "008000";
    google_ui_features = "rc:10";
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </center><br>
    EOFF;
    return <<<EOF';

    $file = "Skin/s1/skin_global.php";

    $contents = file_get_contents($file);

    $contents = str_replace($searchstring, $replacestring, $contents);

    file_put_contents($file, $contents);
    require_once "$file";
    }


    and you difine it to open the file to write
    " // Open the file for writing
    $fh = @fopen($filename, 'w');"
    so i will need a commend!

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, only made a couple of changes (including code layout):
    PHP Code:
    <?php
    $fi
    ="conf_global.php";
    if(
    file_exists($fi)){
        
    $file $_SERVER["PATH_TRANSLATED"];
        if (!
    function_exists('file_put_contents')) {
            function 
    file_put_contents($file$data$respect_lock true){
                
    $fh = @fopen($file'w');
                if (
    $fh === false) {
                    return 
    false;
                }
                if (
    $respect_lock === true && !flock($fhLOCK_EX)){
                    
    fclose($fh);
                    return 
    false;
                }
                if (
    is_array($data)) {
                    
    $data implode(''$data);
                } else {
                    
    $data = (string) $data;
                }
                
    $bytes fwrite($fh$data);
                
    fclose($fh);
                return 
    $bytes;
            }
        }
        
    $searchstring 'global $ibforums;
        return <<<EOF'
    ;
        
    $replacestring 'global $ibforums;
        return <<<EOFF
        <center>
        <script type="text/javascript">
            <!--
            google_ad_client = "pub-3806911217709370";
            google_ad_width = 728;
            google_ad_height = 90;
            google_ad_format = "728x90_as";
            google_ad_type = "text_image";
            google_ad_channel = "";
            google_color_border = "000000";
            google_color_bg = "FFFF66";
            google_color_link = "0066CC";
            google_color_text = "000000";
            google_color_url = "008000";
            google_ui_features = "rc:10";
            //-->
        </script>
        <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
        </center><br>
        EOFF;
        return <<<EOF'
    ;
        
    $file 'Skin/s1/skin_global.php';
        
    $contents file_get_contents($file);
        
    $contents str_replace($searchstring$replacestring$contents);
        
    file_put_contents($file$contents);
        require(
    $file);

    ?>
    Does it output any errors at all? "something is wrong" is not enough to go by.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see a blank page.
    if i will give you the FTP details can you fix it?
    you have icq or msn?

  15. #15
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    *sends PM with msn address*
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •