Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Path

  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Path

    hi im trying to create and write to a file in a folder above where the php file is but it doesn't seem to work.

    code:

    $myFile4 = "players/" + $user + "test.txt";
    $fh = fopen($myFile4, 'w');
    fwrite($fh, "500");
    fclose($fh);

    i dont't see the problem

    any help apreciated
    thanks.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    In php, + is a math operator. The php concatenation operator is a dot .
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    yeh i just thought about that plus sign and changed it to this:

    $myFile4 = $user;
    $myFile4 .= "gold.txt";
    $fh = fopen($myFile4, 'a+');
    fwrite($fh, "500");
    fclose($fh);

    it works but obviously i haven't done the path.

    would this work
    $myFile4 = $user;
    $myFile4 .= "gold.txt";
    $path = "players/"
    $path.=$myFile4;
    $fh = fopen($path, 'a+');
    fwrite($fh, "500");
    fclose($fh);
    Last edited by alcool9999; 10-17-2009 at 04:49 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not if you want to go above this file. To do that, you need to have .. in the filepath. Beware that PHP will work with a current working directory from the executing script, so if you include this into another file that is where it will execute from.
    PHP Code:
    $path dirname(__FILE__) . sprintf('/../%s/gold.txt'$user);
    if (
    false !== ($fh = @fopen($path'a+')))
    {
        
    fwrite($fh"500");
        
    fclose($fh);

    Hopefully you're not using this to track a user's 'gold'. This will start really taking up space on you're server. If you're using something dynamic you should consider the use of a database to take care of you're persistent storage.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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