Allow php write files

fopen

fopen() binds a named resource, specified by filename , to a stream.

Parameters

If filename is of the form «scheme://. «, it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.

If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled open_basedir further restrictions may apply.

If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

Note:

The list of supported protocols can be found in Supported Protocols and Wrappers. Some protocols (also referred to as wrappers ) support context and/or php.ini options. Refer to the specific page for the protocol in use for a list of options which can be set. (e.g. php.ini value user_agent used by the http wrapper).

On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.

The mode parameter specifies the type of access you require to the stream. It may be any of the following:

A list of possible modes for fopen() using mode
mode Description
‘r’ Open for reading only; place the file pointer at the beginning of the file.
‘r+’ Open for reading and writing; place the file pointer at the beginning of the file.
‘w’ Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
‘w+’ Open for reading and writing; otherwise it has the same behavior as ‘w’ .
‘a’ Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
‘a+’ Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
‘x’ Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning false and generating an error of level E_WARNING . If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
‘x+’ Create and open for reading and writing; otherwise it has the same behavior as ‘x’ .
‘c’ Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to ‘w’ ), nor the call to this function fails (as is the case with ‘x’ ). The file pointer is positioned on the beginning of the file. This may be useful if it’s desired to get an advisory lock (see flock() ) before attempting to modify the file, as using ‘w’ could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
‘c+’ Open the file for reading and writing; otherwise it has the same behavior as ‘c’ .
‘e’ Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.

Note:

Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems (Mac OS Classic) used \r as the line ending character.

If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will «look funny».

Windows offers a text-mode translation flag ( ‘t’ ) which will transparently translate \n to \r\n when working with the file. In contrast, you can also use ‘b’ to force binary mode, which will not translate your data. To use these flags, specify either ‘b’ or ‘t’ as the last character of the mode parameter.

The default translation mode is ‘b’ . You can use the ‘t’ mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as old versions of notepad. You should use the ‘b’ in all other cases.

If you specify the ‘t’ flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

Note:

For portability, it is also strongly recommended that you re-write code that uses or relies upon the ‘t’ mode so that it uses the correct line endings and ‘b’ mode instead.

Note: The mode is ignored for php://output , php://input , php://stdin , php://stdout , php://stderr and php://fd stream wrappers.

The optional third use_include_path parameter can be set to ‘1’ or true if you want to search for the file in the include_path, too.

Читайте также:  Accept function in java

Источник

Apache and PHP write permission?

I have a php script that writes to a file. But when I try to actually write to the file I get permission denied. How can I tell what user name I need to add to the file permission in order for the php to write to it?

3 Answers 3

How can I tell what user name I need to add to the file permission in order for the php to write to it?

ps axu|grep apache|grep -v grep 

I gives me www-data . So I just add this user to the group or something liek that? Is that a good solution, or does PHP provides me with something better?

This is completely unrelated to PHP, this is a general server issue (and would belong to serverfault.com). It may be solved in many different ways: giving the file to the user www-data is the most straightforward one, though I can’t know if it’s the best one for you (unless you give us much more detail).

whatever the file is, or directory for that matter, if apache needs to write to it, it needs to be owned by apache , httpd , www-data , or whatever the user apache is running under on your server. you said in a response that it is www-data , so as root, you should do chown www-data filename to change ownership.

i would strongly recommend against changing the permissions to 777, simply because having world readable/writable files and directories on your server can pose some security risks.

ultimately, i would configure your script to write to a set path, then change the ownership of that path to www-data so future files can be created there if need be, without your interaction being required.

Читайте также:  Create new type java

this poses it’s own risks, too, as a compromised script can then write and potentially execute whatever it wants from that directory.

as much of a pain as it may be, doing it file by file is less risky.

Источник

How to give PHP permissions to write to files without globally opening up permissions to the directory

I’m running Centos 5.5. I need to create / write some files with a PHP script. The script only works if I have the permissions of the directory in which the files are to be created / written set to writable for everyone, ie:

The script is working fine, but having my directory writable like that is obviously BAD. My question is, is there a way to allow selected programs (such as PHP) permission to write and create files while keeping everyone else out? Many thanks

1 Answer 1

Make the files/directories PHP needs to write to owned/writable by the user or group that PHP is running as (usually your web server’s user/group).

Note that while this configuration is more secure than world-writable files it is only as secure as your code and your webserver configuration. Plan accordingly.

Hi thanks for your answer. I know I can change the user permission with chown and the group permission wih chgrp (and chown), but could you tell me how I find out which group or groups php is running as please. I know you can use groups but ‘groups php’ isn’t working.

@ablackhat — Thanks for that. I get that the group id and user id is apache. I also did a ‘cat /etc/group’ command which I’ve just read about and apache was in there. But how do I specifically get the user and or groups that php belongs to? Can php belong to anything? I’m assuming I have to change the file permissions inmy directory to apache. But I’m really not sure if I have to do that just for the user or root or both and importantly why? Cheers.

Читайте также:  Python как выучить самому

@Joe — PHP is executed as the user/group running the web server, absent other specific configuration changes.

I’ve changed both the user and the group for the relevant directory to ‘apache’ which has sorted out my problem. What is the difference between changing the group and the user?

Источник

Оцените статью