+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP write variables to file

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    PHP write variables to file

    Hi,

    I have a form, and want to transfer all info typed into another php document as variables!

    Here is the form:
    HTML Code:
    <form action=".index.php" method="get" class="form">
    
    <input type="text" name="title" />
    
    <input type="text" name="brief" />
    
    <input type="submit" />
    
    </form>
    But this php code just writes "0" to the file:
    PHP Code:
    $myFile = "../phpprofiles/pahhh.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = '
    <?php
    $title 
    "'+ $_GET["title"] +'";

    $description "'+$_GET["info"]+'";

    $image "'+$_GET["screenshot"]+'";

    $name "'+str_replace( " ", "", $_GET["title"] )+'";

    $howtostop "'+ $_GET["stop"] +'";

    $tags="'+ $_GET["tags"] +'","'+ $_GET["tags2"] +'","'+ $_GET["tags3"] +'";


    $producer="'+ $_GET["producer"] +'";
    ?>
    ';
    fwrite($fh, $stringData);
    fclose($fh);
    Is there a way to solve this?
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  2. #2
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: PHP write variables to file

    First steps are never easy, so always a good idea to go through all the tutorials. Though must admit there is a distinct lack of intermediate scripting help about for those trying to put it all together. PHP.net is a good place to find basic snippets.

    Here's some quick code for inspiration, handy for many uses including blogging and adding comments to a page. Everthing gets stored in an external file called 'data.txt'.

    PHP Code:
    <?php
    $title     
    stripslashes($_POST["title"]);
    $brief     stripslashes($_POST["brief"]);


    $output '<h2>'.$title.'</h2><p>'.$brief.'</p>';

    if (
    $title != '' && $brief !='') {
    $datFile 'data.txt';//write new data to file
                                    
    $fh fopen($datFile'w') or die("can't open file"); // 'w' = overwite existing file, 'a' = add to file
                                    
    fwrite($fh,$output);
                                    
    fclose($fh);// 
    }

    $content= @file_get_contents("data.txt");
    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body><form action="novice1.php" method="post">
      <table width="330" border="0" cellpadding="5" cellspacing="5">
        <tr>
          <td bgcolor="#FF6600"><h2>Data In File: </h2></td>
        </tr>
        <tr>
          <td bgcolor="#FFCC00"> <?php echo $content?></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td bgcolor="#33CCFF"><label>
            <input name="title" type="text" id="title" value="title" size="30" />
          </label></td>
        </tr>
        <tr>
          <td bgcolor="#0066FF"><label>
            <textarea name="brief" cols="20" id="brief">text description</textarea>
          </label></td>
        </tr>
        <tr>
          <td><label>
            <input type="submit" name="Submit" value="Submit" />
          </label></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP write variables to file

    PHP Code:

    $stringData = '
    <?php
    $title 
    "'



    $_GET["title"] 

    +

    '"
    ;

    $description "'

    +

    $_GET["info"]

    +

    '"
    ;

    $image "'

    +

    $_GET["screenshot"]

    +

    '"
    ;

    $name "'

    +

    str_replace( " ", "", 
    $_GET["title"] )

    +

    '"
    ;

    $howtostop "'



    $_GET["stop"] 

    +

    '"
    ;

    $tags="'

    +

     
    $_GET["tags"] 

    +

    '"
    ,"'



    $_GET["tags2"] 

    +

    '"
    ,"'

    +

     
    $_GET["tags3"] 

    +

    '"
    ;


    $producer="'



    $_GET["producer"] 

    +

    '"
    ;
    ?>
    '    ;
    In PHP '+' is just for addition. So your code turns all the strings into numbers (all 0) and adds them.

    You want '.' to concatenate strings.
    Nothing is always absolutely so.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP write variables to file

    Another option is to use heredoc syntax for the file content. Note that, as written, you've got quite a serious code injection problem. It can be tricky to sanitize the input properly, which is why generating PHP containing user input is a bad idea. One alternative is to generate an INI file, which you can later load with parse_ini_file.

    PHP Code:
    <?php

    $myFile 
    "../iniprofiles/pahhh.ini"
    $fh fopen($myFile'w'); 
    if (! 
    $fh) {
        
    /* don't use "die" if outputting HTML */
        
    ...
    } else {
        
    /* Sanitize input */
        
    if (get_magic_quotes_gpc()) {
            foreach (
    $_REQUEST as $key => $val) {
                
    $_REQUEST[$key] = stripslashes($val);
            }
        }
        
    /* 'strip_tags' may or may not be appropriate, depending on where the data 
         * will end up. For example, some HTML be allowed in the description.
         */
        
    $data str_replace("\n"' 'array_map('strip_tags'$_REQUEST));
        
    $data['title'] = str_replace" """$data["title"]);
        
    # 'array_filter' will remove empty values
        
    $data['tags'] = implode(','array_filter(array_intersect_key(
                                                        array(
    'tags'=>1'tags2'=>1'tags3'=>1),
                                                        
    $data)));
        
    $stringData =<<<EOF
    title={$data["title"]}
    description=
    {$data["info"]}
    image=
    {$data["screenshot"]}
    name=
    {$data["title"]}
    howtostop=
    {$data["stop"]}
    tags=
    {$data["tags"]}
    producer=
    {$data["producer"]}
    EOF;

        
    fwrite($fh$stringData); 
        
    fclose($fh);
    }
    If for some reason you insist on generating a PHP script, look into applying addslashes if magic quotes are off, but it's still dangerous to mix host and embedded languages when generating output.
    Last edited by misson; 04-10-2011 at 02:28 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Cannot Create or edit File (No write access)
    By shahalpk82 in forum Free Hosting
    Replies: 0
    Last Post: 04-06-2011, 09:14 AM
  2. Failed to Write File To Disk Error
    By LameRock in forum Free Hosting
    Replies: 3
    Last Post: 11-16-2010, 05:56 PM
  3. MySQL error - can't create/write to file
    By estong13 in forum Free Hosting
    Replies: 15
    Last Post: 08-20-2009, 05:20 AM
  4. what permissions do I need to use php to write to file?
    By diabolo in forum Programming Help
    Replies: 14
    Last Post: 07-31-2009, 04:02 AM
  5. over write files via file manager
    By tittat in forum Free Hosting
    Replies: 3
    Last Post: 01-28-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers