Search code examples
phpubuntufopen

unable to open file with php on ubuntu machine


I trying to create new file in Ubuntu system using PHP script, but when I run this script the error

Unable to Open file 

is appear although I sure that the file's path is right and I have permissions to access this file I don't know where is the wrong this is my code

$myfile = fopen('inc/users/future.php', "w")or die("Unable to open file!") ;
$text='<?
$host ="'.$host.'";
 $user ="'.$db_admin.'" ;
 $pass ="'.$db_password.'";
 $db ="'.$database.'" ;
$myconn;?>';fwrite($myfile, $text);
fclose($myfile);

the path of this script is

/var/www/html/ghost/index.php

and the path of the file which I wish to open is

/var/www/html/ghost/inc/users/future.php

in other hand when I run this script in windows machine every thing is go fine


Solution

  • In your script use

    fopen(dirname(__FILE__) . '/inc/users/future.php', 'w')
    

    This will create a filepath from the directory your index.php. If you script is called from another file, php might search coming from that file.

    Also check if the php process has sufficient file permissions to read and open the file. Try setting the file to chmod 777 just to test if that is the case (do not leave it on 777 though).

    Keep in mind that if the file is a symbolic link, the 'w' parameter of fopen will not work.