Search code examples
phpfilecookiesdirectoryfopen

Failed to open stream: No such file of directory in... PHP fopen


this is my code

include 'connection.php';
setcookie('username',$name,time()+86400,"/");
echo $_COOKIE['username'];
$my_file = '/../../users/'.$name."/index.php";
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);

when the code runs I want it to create a file with the directory /../../users/[username] where [username] is stored in the $name variable. When I run my code it just gives me a lousy error and it doesn't run any code after line 6 from above.

It won't create the file and the error is...

Warning: fopen(/../../users/codemaster7/index.php) [function.fopen]: failed to open stream: No such file or directory in /nfs/c02/h10/mnt/45563/domains/pizzascripters.com/html/projects/nik/Champion/register/index.php on line 107 Cannot open file: /../../users/codemaster7/index.php

Does anyone have an idea on whats going on and how I can fix it?


Solution

  • 1 include 'connection.php';
    2 setcookie('username',$name,time()+86400,"/");
    3 echo $_COOKIE['username'];
    4 $my_file = 'index.php';
    5 $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
    

    I update the directory of file to just a file name in line 4. if you wanna use the directory as a parameter then use the function file($myfile); which return the array of data from your file.