Invalid permission file_put_contents

I am trying to write a file request for debugging. The file is located in database/execute.php . The file I want to write is database/queries.php .

I am trying to use file_put_contents('queries.txt', $query)

But I get

file_put_contents (queries.txt) [function.file-put-contents]: could not open the stream: Permission denied

I have a queries.txt chmod'd file before 777, what could be the problem?

+84
php file-io file-permissions
Feb 07 '11 at 3:14
source share
13 answers

Try setting directory permissions.

from the terminal, run chmod 777 database (from the directory containing the database folder)

apache, and no one will have access to this directory if it is correctly chmodd.

Another thing is echo "getcwd ()". This will show you the current directory, and if it is not "/something.../database/", you will need to change "query.txt" to the full path for your server.

+65
Feb 07 2018-11-11T00:
source share

Another variant

is that you can make Apache (www-data) owner of the folder

 sudo chown -R www-data:www-data /var/www 

which should now work file_put_contents . But for greater security, you should also set permissions as shown below.

 find /var/www -type d -print0 | xargs -0 chmod 0755 # folder find /var/www -type f -print0 | xargs -0 chmod 0644 # files 
  • change /var/www to the root folder of your php files.
+13
May 23 '17 at 14:42
source share

Understand that now it is quite old, but there is no need to manually write requests to such a file. MySQL has built-in logging support, you just need to include it in the development environment.

Take a look at the documentation for the "general query log":

http://dev.mysql.com/doc/refman/5.1/en/query-log.html

+7
Mar 17 '13 at 12:41
source share

Guys, I had this problem for 1 month, and I did everything, but could not fix it, but now I know the solution.

I use shared linux hosting when my admin changed php to 5.3. I got a lot of errors for the code "file_put_contents". try checking my plan:

In your host, create a file, for example mytest.php, and paste this code and save:

 <?php mail('Your-EMail','Email-Title','Email-Message'); ?> 

Open the URL "www.your-domain.com/mytest.php" once, and then check your email. you must have an email from your host with the information you entered in mytest.php, check the sender name. if you have None , you have a problem with the “Resolving Failure” because something is undefined and if the sender's name looks like my id: iietj8qy@hostname5.netly.net, you have no problem.

My administrator changed the server and installed the node again. I think the problem is resolved, tell the administration of your hosting what I told you, and maybe they will find the answer.

Hope this helps you!

+3
Feb 06 '14 at 15:57
source share

I know this is a very old question, but I wanted to add a good solution with some detailed explanations. You will need to complete two statements on similar Ubuntu systems, and then it works like a charm.

Linux permissions can be represented by three digits. The first digit defines the permission of the file owner. The second figure is the rights of a certain group of users. The third digit defines permissions for all users who are not the owner or member of the group.

It is assumed that the web server will run with an identifier that is a member of the group. The web server should never start with the same identifier as the owner of the files and directories. Ubuntu runs apache under id IDs. This identifier must be a member of the group for which permissions are specified.

To provide the directory to which you want to change the contents of files, the appropriate rights, execute the statement:

 find %DIR% -type d -exec chmod 770 {} \; 

. This would mean in the question that the permissions for the% ROOT% / database directory need to be changed accordingly. Therefore, it is important not to have files inside this directory that should never be changed or deleted. That is why it is recommended to create a separate directory for files whose contents need to be changed.

Read permissions (4) for the directory mean the ability to collect all files and directories with their metadata in the directory. Write permission (2) gives permission to change the contents of the directory. This means that you add and delete files, change permissions, etc. Permission to execute (1) means that you have the right to go to this directory. Without the latter, it is impossible to go deeper into the catalog. The web server needs read, write, and execute permissions when the contents of the file change. Therefore, the group requires the number 7.

The second statement is in the OP question:

 find %DOCUMENT_ROOT%/database -type f -exec chmod 760 {} \; 

Reading and writing a document is required, but the execution of this file is not required. 7 is provided to the file owner, and 6 to the group. The web server does not need to have permission to execute the file in order to modify its contents. These write permissions should only be specified in files in this directory.

All other users should not receive any permissions.

For directories that do not require changing its files, group permissions are permissible 5. Permission documentation and some examples:

https://wiki.debian.org/Permissions

https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

http://www.linux.org/threads/file-permissions-chmod.4094/

+3
Dec 21 '15 at 18:30
source share

Gathering information at this link https://stackoverflow.com/a/4646262/ and from user azerafati and Loek Bergman

if you want to see the file / etc / apache / envvars, you will see something like:

 export APACHE_RUN_USER=www-data export APACHE_RUN_GROUP=www-data 

Apache starts under the username www-data p>

'0755' means that the file owner can read / write / execute, but groups and other users cannot write. therefore, in the terminal ur, cd to the folder containing your "images" folder. then enter:

 find images -type d -exec chmod 0755 {} \; find images -type f -exec chmod 0755 {} \; sudo chown -R www-data:www-data images 

You must first change gear before changing ownership. enter your password when prompted. this will be done by the owner of the www-data image folder.

Your download should now work.

+3
Jul 14 '17 at 10:29
source share

For those using Ubuntu and getting this error while loading the page locally, but not in the web hosting service,

I just fixed it by opening nautilus ( sudo nautilus ) and right-clicking on the file you are trying to open, click Properties> Preferences> and give read write for everyone else

+1
Oct 20
source share

there was the same problem; my problem was that selinux was forcibly installed.

I continued to receive the error message "Could not open stream: access denied" even after switching to 777 chmoding and verifying that all parent folders have execute permissions for the apache user. Turns out my problem was that selinux was configured to force execution (I'm on centos7), it is devbox, so I turned it off.

0
Mar 13 '18 at 17:58
source share

This can be solved using the following steps:

 1. $ php artisan cache:clear 2. $ sudo chmod -R 777 storage 3. $ composer dump-autoload 

Hope help

0
Apr 25 '19 at 9:45
source share

If you download git from local to the server, you sometimes need to clear the cache due to the view files that it loads with it / or other cached files.

 php artisan cache:clear 

It can sometimes happen that your application worked before git pull

0
May 14 '19 at 19:44
source share

There are 2 ways to solve these problems.
1. use chmod 777 path-to-your-directory . If it does not work, then
2. Just provide the full path to the query.txt file.

-2
Jan 12 '17 at 5:47 on
source share

Here is the solution. To copy img from url. this url: http://url/img.jpg

 $image_Url=file_get_contents('http://url/img.jpg'); 

create the desired path, end the name with .jpg

 $file_destino_path="imagenes/my_image.jpg"; file_put_contents($file_destino_path, $image_Url) 
-3
Jun 20 '15 at 2:33
source share

Also, as stated in the file_put_contents man page at php.net , beware of name issues.

 file_put_contents($dir."/file.txt", "hello"); 

may not work (although this is also true in the syntax), but

 file_put_contents("$dir/file.txt", "hello"); 

works. I tested this on different installed php servers.

-eleven
Nov 17 '11 at 6:33
source share



All Articles