Php letter to file - empty?

I struggled with writing one line to a file. I use simple code under Slackware 13:

$fp = fopen('/my/absolute/path/data.txt', 'w'); fwrite($fp, 'just a testing string...'); fclose($fp); 

A file is created (if it has not already been created), but is it empty ?! The directory in which this file is written belongs to the user and the apache group (daemon.daemon) and has permissions 0777. This has never happened to me before. I am curious what is the reason I cannot write inside a file?

Thanks in advance.

+4
source share
4 answers

Try $ df -h

Your disk is probably full.

+6
source

In my opinion, you can check the return values:

 $fp = fopen('/my/absolute/path/data.txt', 'w'); // $fp -> manual: "Returns a file pointer resource on success, or FALSE on error." if ($fp) { $bytes_written = fwrite($fp, 'just a testing string...'); if ($bytes_written) { echo "$bytes_written bytes written!\n"; } else { echo "Error while writing!\n" } $success = fclose($fp); if ($success) { echo "File successfully closed!\n"; } else { echo "Error on closing!\n"; } } else { echo "No filepointer ressource!\n"; } 
+2
source

I suggest using file_put_conents ($ file_name, $ file_cotents); And to get the content: file_get_contents ($ file_name);

The code looks cleaner.

http://php.net/manual/en/function.file-put-contents.php and http://www.php.net/manual/en/function.file-get-contents.php

+1
source

Maybe something happens to the script / file before closing the file. Check if there are other processes trying to access the file (you can use lsof ). Also try writing to a new file to see if the same thing is happening.

Also check the return value on fclose() to make sure the file is successfully closed.

+1
source

Source: https://habr.com/ru/post/1338640/


All Articles