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;
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.
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.
It is because of SELINUX.
You can mark the folder as of type httpd_sys_rw_content_t with this command to enable writing to this folder.
semanage fcontext -a -t httpd_sys_rw_content_t '/files(/.*)?'; restorecon -RF '/files/'
We've experienced this, requiring a workaround (regardless of method, permissions and anything else mentioned here). When all other fixes failed, we have found it can be linked to restrictions created by SELinux.
If you’re using windows the following solution worked perfectly for me on Windows 10 running php 5.5.38
If you’re having this problem on Windows/IIS try the following:
Go to the Folder that you are trying to write to and right click it, then select properties.
Select the Security Tab
Click Edit
Click Add
Click Advanced
Click Find Now
From the list of User select IUSR and click OK
Click OK again.
The IUSR will be displayed in the top box labelled 'Group of User Names'
Select IUSR and grant necessary permissions in the 'Permissions for BATCH' list view.
Click Apply and then you are done.
The steps may be slightly different for different versions of windows. This also applies to ASP.NET though I think the users you add are the network users (NETWORK AND OR NETWORK SERVICE users) as well as the IUSR.
Related
I'm facing a problem with deleting video files from folder using php unlink() function , image are deleting but when trying deleting videos it says
unlink(file_path) : permission denied.
You (running your script as either through CLI or a webserver) need write access to the directory in which the files are located. so access to the file is not enough.
Your image directory would be different and writable for webserver or cli.
chmod("your/video/dir/path",0777);
try using above code before unlink the video in your script.
Edit: Looks like you are using Windows. Unfortunately, my answer is for Unix-like operating systems (so Linux, MacOS). You could try installing Bash extension for Win8+, but still I'm not sure if this would work. However, I'm keeping my answer in case of anyone with similar problem is looking here for an answer.
Changing permissions through PHP might work in some cases, but not always, because if you don't have permissions to delete the file you might also not have permissions to change them.
The best solution is to create a directory where you will keep files to which PHP will have full access. Let's call it dirname. Once you have created a directory, change its owner and group to the one corresponding to your web server user's name (if you're using Apache, it's "www-data"), for example: chown www-data:www-data dirname.
Once you've done that, change folder's permissions. My suggestion is 744, it will assure that user who owns it will have all permissions, and everyone else will be able only to read it. To do that, execute the following command: chmod -R 777 dirname.
Now you should be able to do anything you want with the files in given directory directly from PHP.
I have a project I need to generate all sorts of PDF documents for with PHP, so I searched and found TCPDF. All seemd OK until I got to saving files to some folder.
So, I created the needed folder, set 0777 permissions to it, then proceded with generating a file into it. The file was created and all looks good, but the file now belongs to the 'daemon' user, and has 0644 permissions.
I need the generated files to have 0777 permissions. I dont' mind the 'daemon' user as owner, but it would be cool to also know how to set it to the 'root' user.
I searched the official documentation to TCPDF for an answer, but failed to find one, so I came here to ask for help.
So, can anyone help me with some sugestions?
I have solved this problem in my Apache installations on Debian Server by setting the umask environment variable in /etc/apache/envvars.
In my case all I wanted was for the the user and group to have write permissions. I achieved this by setting the umask variable in that file to 007. The mask is applied to the permissions that are inherited by whatever user Apache is running under and so in my case the net result is that all files created by PHP within Apache have 660. I guess in your case you might need 000 (or 0000).
There is absolutely no point setting the execute permission and I would advise against it, because at best it achieves nothing and at worst it might introduce some strange security issue.
This answer contains more info on setting the umask: Setting the umask of the Apache user
The only benefit of this solution over PHP's chmod function is that you can "set it and forget it", the problem being that if you forget to set it then you have a problem.
I currently have a php script that is running when a browser browser browses to the webpage. What I'm trying to do is write a text file when the script runs that stores a variable. The owner of the folder is apache, but everyone has read write, strictly for testing purposes. (I thought it might be a permissions issue) SELINUX is enabled on the server, and when I run the script from console it creates the text file just fine, and in the right directory.
file_put_contents("My working file location", $myString);
I'm using this line to try to write and create the text file, I know that my file location works becaus I can run it and create it in offline mode, I.E. running it through console. The problem is that the variable I'm trying to write is populated through HTTP Post, and when I run the script through the browser, or when apache runs the script, it does not write or create the file. What do I need to do to allow access to write/change syntax wise to get this script to write this text file?
Your problem is likely due to apache not having permissions to write to the file location you specified. Go to that directory and check the permissions and group ownership with the ls command:
cd "My working file location"
ls -l .
There are three columns in the output that show the permissions, owner, and group for the directory. Most likely they are owned by root and don't have permissions for apache to write to the directory.
If this is the case, then you will see an error appear in your apache log when it tries to create the file. Try tailing your logs while running the script in your browser:
tail -f /var/log/apache2/error.log
I had the same trouble recently and stumbled upon this question. Unfortunately choppyfireballs the OP said in a comment he found his own solution and just accepted an answer that wasn't helping any of us... Then after a search and a success to make file_put_contents work again I decided to share my solution.
The permissions of my files and directories were ok to accept any writing (make sure your directories are chmod 757 this will give the root and others the grant to write files in the location). If it still doesn't work like it didn't for me, that's because your system is probably SELinux (Security Enhanced Linux) system.
If you want to make sure write setenforce 0 this will turn selinux to permissive mode, run your script again, if it works then it means the problem is well described.
In that case turn selinux on back setenforce 1 and try ls -Zl in the directory where the directory of your project is. this will give you a line like
drwx---r-x. 9 root root system_u:object_r:httpd_sys_content_t:s0 4096 Dec 8 00:25 project
or something different but httpd_sys_content_t if you used chcon to transfer the context from one directory to this one. but if you don't have httpd_sys_content_t it's ok because we need to change the context of that directory anyways.
first you need to accept any public_content_rw_t contexts to write file. Type
setsebool -P httpd_anon_write on
This will set (P)ermanently SELinux boolean httpd_anon_write to true and any context dubbed as public_content_rw_t will have the rights to write any files in their own location.
Now you have to say SELinux that your project directory is public_content_rw_t or you'll still not be able to write files. Type :
semanage fcontext --add --type public_content_rw_t "/project(/.*)?"
and restorecon -RvF /project to tell selinux to apply the above specifications.
Now your directory is public_content_rw_t and you should be able to write files.
I ran into this problem too. In my case, I found that the ownership of the directory was wrong. For a typical Apache installation the directory should be owned by www-data:www-data, not root:root.
Something else to try, for people with a similar question. You might just be making a simple mistake that doesn't require you to mess around with the file permissions—and if you're making this mistake, fixing the file permissions might not help.
Be sure you're using a local, relative file path in file_put_contents().
For example, use:
file_put_contents('short_local_path/my_working_file.txt', $myString);
Not:
file_put_contents('http://example.com/remote_path/my_working_file.txt', $myString);
And not:
file_put_contents('/whole/root/file/path/to/my_working_file.txt', $myString);
Have you tried chmodding the directory to 777?
Try this:
if(file_put_contents('file.txt', 'text')){
die('yes');
} else {
die('no');
}
Might of misspelled something. ^
My code
mkdir("/some/absolute/path",0777);
and
mkdir("relative/path", 0777);
is not working, safe mode is turned off, and I've even tried setting all of the parent folders to 777.
Any ideas?
EDIT: I do have error reporting turned on, in my frustration I've 777'd the whole path just to make sure that, that isn't the issue. It's gotta be something stupidly simple going on.
EDIT EDIT: Upvotes for everyone who responded with suggestions... But I'm not going to select an answer since this still isn't resolved, but then again I think this is going to be one of those ones left open forever.
EDIT x 3: So I have the most unsatisfactory resolution to this question ever... I started with a clean VM image, retried it and it works now. No joke.
Do all of the parent directories exist?
If not, you'll need to enable recursion (assuming PHP5 here):
mkdir('/path/to/your/dir',0777,true);
EDIT: Didn't see the hidden comment saying that every directory from var downward was set to world-writable, so I'm betting the directory path exists and the above won't be helpful. Sorry!
Are you trying to create those directories recursively, like you would do with mkdir -p on the command line? If so, specify true as the third parameter to mkdir.
And just to echo the previous suggestions, PLEASE specify the error messages you are getting. If you are not getting any, use this before your call: error_reporting(-1); // ALL messages and ini_set('display_errors', 'On');.
If anyone gets stuck with this problem.. there's one answer I can give you that I spend 2 hours on finding.. I tried with using a full path, and "../mydirectoryname".
Try using:
mkdir("./mydirectoryname", 0777, true);
Instead of..
mkdir("../mydirectoryname", 0777, true);
Have you tried with the shortest test possible?
mkdir('directory',0777);
If this does not work I would try creating with a standard CHMOD like 0755 (this is a totally random guess, maybe the server won't allow creating 0777 via PHP)
if this don't work I would say the server probably need different setup / php doesn' thave the writting right on folder, maybe you could ask your host provider?
I have similar problem and I found out, that I have no free space left on my drive. Check with command df (on linux) how full is your drive. It is possible that root is allowed to create files and folders in this situation, because he has pre-reserved space. If you run you script from command-line as root user - there is no error, but if your script is run by apache, then error occure.
For future references, the problem might come from the possibility that the directory where you're trying to create your new directory doesn't have enough permission.
For instance, your index directory might look like this:
index.php new_dirs_here
if new_dirs_here doesn't have enough permission then you can't create direcories inside.
To solve this, I'd use the command:
chmod 777 new_dirs_here
I'm not worrying about security now, Just trying to solve the immediate problem. You could of course look up a better permission settings, but the idea is that your new_dirs_here should have enough permissions.
Then, your mkdir() dunction should work just fine.
Good luck
Make sure the parent directories have correct write permissions, that was my problem
mkdir only creates one single directory when called without -p.
A directory in the path /usr/local/myfolder/ is missing, this is why you get the error. If you call mkdir -p, the missing path is created as well.
Another effect of using the -p option is that mkdir -p does not complain when the directory already exists. This is why this variant is frequently used in scripts.
chown www-data:www-data (address directory)
You're missing quotes around the path name parameter.
You must take the attribute in quotes:
mkdir('path/to/your/dir','0777');
I am creating an image dynamically using php. The image is being created if I go in and make the file first. However, if I don't create the file manually then I get the following error.
Warning: imagegif() [function.imagegif]: Unable to open 'filename2.png' for writing in /www/vhosts/yourraceresults.com/htdocs/trial/TextToImage.inc.php on line 144
Example:
public function SaveTextAsPng($fontSize, $x, $y, $textContent, $fileName='image'){
imagestring($this->image, $fontSize, $x, $y, $textContent, $this->text_color);
return imagepng($this->image, "/www/vhosts/yourraceresults.com/htdocs/admin/trial/images/".$fileName.".png");
}
$textToImage->SaveTextAsPng(10, 11,11, 'fakeinfo','filename2');
Either the output folder do not exist (in which case you have to create it), or you do not have write permissions to the output folder. To fix this:
If you're using a shell:
chmod 777 /www/vhosts/yourraceresults.com/htdocs/trial
If you're using an FTP client:
Right click on the "trial" folder, search for something called "chmod", "rights", "permissions" (perhaps in "properties"), then give it every permission or, alternatively, enter chmod value 777.
It seems your web user (apache, www, ...) has not got write permissions in the folder you are trying to write to. Or, if the file already exists, it has not got write permissions for that file.
Edit: Changing the permissions
Supposing you are on a linux like system with an apache web-server, first you need to find out as which user your web-server is running. You need to get a command shell on the server (ssh) and then you can probably see the web-server user typing the following command:
$ ps aux | grep httpd
In the list you will see all running apache processes with the username in the first column.
Let´s say that the web-user is apache, you now need to give apache write permissions in that directory. You can do that by changing the group to apache and giving write permissions to that group or by changing the ownership of the directory to apache. Let´s say you want to change the group:
$ chgrp apache /www/vhosts/yourraceresults.com/htdocs/trial
Give the owner and the group users (apache) write permissions:
$ chmod 775 /www/vhosts/yourraceresults.com/htdocs/trial
Disclamer: this is a fast solution, but I don´t know much about the safety, comments are welcome!
Do you have properly setted rights for output dir? I'm think your web server (and php with it) can't write to destination folder.
It seems your script needs write permissions on that directory. An easy way to change these permissions is to login via FTP and change the permissions from your FTP client. Most (at least, of the few I've tried) FTP clients offer an option when you right-click a folder called "Permissions" or "File Attributes" (FileZilla) or something like that. It should show a dialog with different permissions checkboxes; make sure to give at least the Owner permission to write. If you are on a *nix host, you should be able to go to the directory and issue the command chmod 755 . to change the permissions.
EDIT: chmod 777 . will give you the most permissions, if 755 does not work.