Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php file_put_contents function not working

Why would file_put_contents refuse to work for the following code?

$f = file_put_contents("files/r.js", "Some text here");  if ($f) print 1; else print 0; 
like image 402
steve Avatar asked Jan 02 '11 04:01

steve


People also ask

What is file_ put_ contents in PHP?

The file_put_contents() writes data to a file. This function follows these rules when accessing a file: If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename. Create the file if it does not exist.

How create file if not exist in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do you check is file exist in PHP?

The file_exists() function checks whether a file or directory exists.


2 Answers

It could be a permission problem

Is the directory /files chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.

Try to see if it has enough permissions, if not, then set it to 777 and try it.

like image 64
Famver Tags Avatar answered Sep 28 '22 00:09

Famver Tags


Are you using the full path on the filesystem or are you trying to use the URI? I think this PHP function expects you to give the path as the file is found on the filesystem.

Relative paths should be okay though.

You may need to make sure the file exists and that it's permissions are set to 777. Sometimes I've found that it's not enough to just have the directory permissions set to 777 but the file must also already exist.

like image 45
jmort253 Avatar answered Sep 28 '22 00:09

jmort253