Results 1 to 8 of 8

Thread: PHP help

  1. #1
    Join Date
    Dec 2007
    Posts
    2,807
    Tokens
    0

    Latest Awards:

    Default PHP help

    Ok, I just need help with this and then I'm done

    <?php
    $x = $_POST['position'];
    $user = $_POST['user'];

    $myFile = <here>;
    $fh = fopen($myFile, 'w') or die("can't open file");

    fwrite($fh, $x);
    fclose($fh);

    echo "success";

    ?>
    Firstly I want myFile (the bit in bold) to equal "user".txt so if user = John then myFile John.txt, how would I do that?

    Secondly I want it so if the textfile doesn't exist then it creates it with permissions "7 7 7".

    Could someone maybe tell me how to do it and then the PHP side of things is all done

  2. #2

    Default

    Quote Originally Posted by FlyDuo View Post
    Ok, I just need help with this and then I'm done


    Firstly I want myFile (the bit in bold) to equal "user".txt so if user = John then myFile John.txt, how would I do that?

    Secondly I want it so if the textfile doesn't exist then it creates it with permissions "7 7 7".

    Could someone maybe tell me how to do it and then the PHP side of things is all done
    PHP Code:
    <?php
    $x 
    $_POST['position'];
    $user $_POST['user'];

    $myFile $user '.txt';

    $fh fopen($myFile'w') or die("can't open file");

    fwrite($fh$x);
    fclose($fh);

    if ( !
    file_exists$myFile ) )
    {

         
    chmod$myFile0777 );

    }

    echo 
    "success";

    ?>
    Did that off the top of my head, try it!
    Last edited by Jam-ez; 24-06-2009 at 05:15 PM.

  3. #3
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    So James you only chmod the file if it doesnt exist, that makes total sense.
    Hi, names James. I am a web developer.

  4. #4
    Join Date
    Dec 2007
    Posts
    2,807
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Jam-ez View Post
    PHP Code:
    <?php
    $x 
    $_POST['position'];
    $user $_POST['user'];
     
    $myFile $user '.txt';
     
    $fh fopen($myFile'w') or die("can't open file");
     
    fwrite($fh$x);
    fclose($fh);
     
    if ( !
    file_exists$myFile ) )
    {
     
         
    chmod$myFile0777 );
     
    }
     
    echo 
    "success";
     
    ?>
    Did that off the top of my head, try it!
    Thanks a bunch! REALLY REALLY helpful, its people like you that make this forum great.

    Quote Originally Posted by Protege View Post
    So James you only chmod the file if it doesnt exist, that makes total sense.
    I wondered that too, maybe he forgot to put the "create file" bit in first if it doesnt exist. Any ideas how I can sort that out?

  5. #5
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    <?php
    if( ! file_exists( $myFile ) )
    {
    chmod( $myFile, 0777 );
    }

    $fh = fopen( $myFile, 'w+' );
    Hi, names James. I am a web developer.

  6. #6
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    <?php
    if( ! file_exists( $myFile ) )
    {
    chmod( $myFile, 0777 );
    }

    $fh = fopen( $myFile, 'w+' );
    You're still chmoding it even though it doesn't exist?

  7. #7
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    I wasnt paying attention to what I was doing sorry.

    PHP Code:
    <?php
    if( ! file_exists$myFile ) )
    {
        
    $fh fopen$myFile'w' );
        
        
    chmod$myFile0777 );
    }
    else
    {
        
    $fh fopen$myFile'w' );
    }
    Hi, names James. I am a web developer.

  8. #8
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Oh the irony

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •