file_put_contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

file_put_contents

Post by BomBas »

Hi, I am trying to put some contents in a file.

I am using this code:

Code: Select all

    $file = APPPATH. 'views/'. $row->skin. '/'. strtolower($this->skin). '.php';
$Data = nl2br( $_POST['data'] );
    file_put_contents( $file, $Data, LOCK_EX );
Let's say $_POST['data'] is:

Code: Select all

 
<html>
<body>
</body>
</html>
 
It inserts blank data into the file - always.
I tried using fopen and fwrite also, doesn't work.

Somebody knows? :) thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file_put_contents

Post by requinix »

Have you checked that $file and $data are what you think they are?
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: file_put_contents

Post by BomBas »

Yes, of course. By the way:
My files aer encoded in UTF-8 without BOM. How can I save the data as UTF-8 also?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file_put_contents

Post by requinix »

Are you sure that the directory where the file should be has the right permissions to allow PHP to create files?

To save as UTF-8 just make sure that the $Data is UTF-8 encoded.
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: file_put_contents

Post by BomBas »

Figured that out somehow, thanks. :)

Now another question:
I want to get this

Code: Select all

if( ... )
{
...
}
To:

Code: Select all

<if="...">...</if>
And if it's with else, to make <else></else>

I tried this code:

Code: Select all

 
                $current = preg_replace("#if\s*\((.+?)\)\s*\{(.+?)\s*\}#is", "<if=\"\\1\">\\2</if>", $current);
 
Didn't work... Thanks in advance! :)
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: file_put_contents

Post by BomBas »

First of all, thanks. I took your code and did some changes.
Here is the new code:

Code: Select all

 
<?php
 
$bad_parse = "if( \$var = \$var )
{
    echo 'some';
}
else
{
    echo 'other';
}";
echo block_parsing_example( $bad_parse );
function block_parsing_example( $input )
{
    // Matches if, else, elseif, else if blocks
    // - Doesn't work with functions in the condition
    // - Doesn't work with comments immediately after condition
    // - Doesn't work with } in strings, comments, or nested blocks
    // - Doesn't work with blocks without {}
    // - Doesn't work with multiple whitespace characters in strings
    if ( preg_match_all('/((if|else\s*if)\s*\([^\)]+\)|else)\s*\{[^\}]*\}/i', $input, $blocks) )
    {
 
        foreach ($blocks[0] as $block)
        {
            //$output .= '<pre class="b">';
           
            $closing_tag = '';
           
            // Matches "elseif" or "else if" condition
            if (preg_match('/else\s*if\s*\([^\)]+\)/i', $block, $condition))
            {
                // Cleans condition statement
                // 1. Adds <elseif="
                // 2. Removes elseif(
                // 3. Removes white space around condition
                // 4. Removes closing )
                // 5. Adds >
                $output = '<elseif="'.str_replace('"', '\"', trim(substr(preg_replace('/else\s*if\s*\(/i', '', $condition[0]), 0, -1))).'">';
                $closing_tag = '</elseif>';
            }
            // Matches "if" condition
            else if (preg_match('/if\s*\([^\)]+\)/i', $block, $condition))
            {
                // Cleans condition statement
                // 1. Adds <if="
                // 2. Removes if(
                // 3. Removes whitespace characters around condition
                // 4. Removes closing )
                // 5. Adds >
                $output = '<if="'.str_replace('"', '\"', trim(substr(preg_replace('/if\s*\(/i', '', $condition[0]), 0, -1))).'">';
                $closing_tag = '</if>';
            }
            // Matches "else" condition
            else
            {
                // 1. Adds <else>
                $output = '<else>';
                $closing_tag = '</else>';
            }
           
            // Cleans statements in block
            // 1. Removes condition and closing brace
            // 2. Escapes double quotes
            // 3. Converts multiple whitespace characters to a single space
            // 4. Trims leading and trailing whitespace characters
            //$content = trim(preg_replace('/(\h*\v\h*)|\h+/', ' ', str_replace('"', '\"', substr(preg_replace('/((if|else\s*if)\s*\([^\)]+\)|else)\s*\{/i', '', $block), 0, -1))));
//$content .= preg_replace("#(if|else|else\s*if)+(\s|\n)*{(.+?)(\s|\n)*}#", $output. "\\1". $closing_tag, $block);
$in_state .= preg_replace("#(if|else\s*if|else)\((.+?)\)+(\s|\n)*[\{]*(\s|\n)*(.+?)(\s|\n)*[\}]*#i", "\\5", $block);
$content .= str_replace($block, $output. $in_state. $closing_tag, $block);
           
            //$content .= $closing_tag;
            //$output .= "</pre>\n";
        }
    }
    return $content;
}
 
?>
 
Returns:
<if="$var = $var">echo 'some'; }</if><else>echo 'some'; }else { echo 'other'; }</else>
How can I fix it? By the way, I'll be glad if you could get rid of the <?php and ?> (by regex of course)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: file_put_contents

Post by jmut »

This seems quite complex. Have the feeling you're going the wrong way. Care to explain what you need all this for?
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: file_put_contents

Post by BomBas »

Templates edit from the control panel.

I am open for other suggestions. :)
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: file_put_contents

Post by BomBas »

I made it :)

Code: Select all

        $current = trim( preg_replace("#if\s*\((.+?)\)[\s|\n]*\{[\s|\n]*(.+?)[\n\s]*\}#is", "<if=\"\\1\">\n\\2\n</if>", $current) );
        $current = trim( preg_replace("#else\s*[\s|\n]*\{[\s|\n]*(.+?)[\n\s]*\}#is", "<else>\n\\1\n</else>", $current) );
        $current = str_replace("<php", "php", $current);
 $current = str_replace("<?", "php", $current);
 $current = str_replace(">", "/php", $current);
 
But I am still open for other suggestions.

EDIT:

The edit is still not working.

Code: Select all

 
    $file = APPPATH. 'views/'. $row->skin. '/'. strtolower($this->skin). '.php';
$Data = stripslashes( $_POST['data'] );
if ( is_writeable($file) )
{
        $f = fopen($file, 'w+');
        if ($f !== FALSE)
        {
        $Data = str_replace("<", "<", $Data);
        $Data = str_replace(">", ">", $Data);
        $Data = preg_replace("#<if=\"(.+?)\">\n(.+?)\n</if>#is", "if(\\1)\n{\n\\2\n}", $Data);
        $Data = preg_replace("#<else>\n(.+?)\n</else>#is", "else\n{\n\\1\n}", $Data);
        $Data = str_replace("php", "<?php", $Data);
        $Data = str_replace("php", "<?", $Data);
        $Data = str_replace("/php", "?>", $Data);
        fwrite( $f, $Data );
        }
        fclose($f);
}
 
Everything is alright, the directory, the permissions and the file.. but it is not writing to the file.
Post Reply