Search code examples
phpfwrite

PHP - Cannot write in a file


I want to write in a file with php but nothing seems to work :(

I tried everything i've sawn on internet :

this:

$file = fopen('text.txt', 'a+');
fputs($file, "a text");
fclose($monfichier);

and this :

$file = fopen('text.txt', 'a+');
fwrite($file, "a text");
fclose($monfichier);

and this :

file_put_contents("text.txt", "a text");

i also tried to put a sleep(1); before the fclose but it didn't change anything

my file and directory got 777 permissions, there is no error message, the functions don't return any error but the file is always empty u_u.

Can someone help please ?


Solution

  • This is the most relevant and not erroneous code you show. (all your code before have some invalid syntax)

    file_put_contents("text.txt", "a text");
    

    But this must normally work, if this doesn't work too, i suggest you to looking for allowed path to write works.

    You can edit for add it in you php configuration file (php.ini) Find where are located you php.ini

    from windows console

    php --ini
    

    or by echoing it with php builtin function

    php_ini_loaded_file();
    

    Verifying in your php.ini (openbase_dir Doc's page) and ad the full path when you want php are allowed the correct rights to manipulate files.

    open_basedir = C:\you_path
    

    http://php.net/manual/en/ini.core.php#ini.open-basedir

    And please check you error log, may be the solution is simply into you logs :) Then in php.ini, again, check if this line is correctly filled

    error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
    

    And do not forgot to restart your bundle httpd + php daemon before testing !