Solving fopen() - permission denied on remote server without 777 - php

I find it hard to believe that this question doesn't have an answer here, but I've searched and searched, and I can't find one.
I'm using fopen() on a remote server, and I only have FTP access - nothing else.
When I try to create or open a file for write access, I'm hit with the following error:
Warning: fopen(entries/newsletter.csv): failed to open stream: Permission denied in
/var/www/sites/xxx/public_html/index.php on line 24
Error: Can't open file. Your entry was not saved.
I've tried changing the file permissions to 777 and opening the existing file, and it works! But that cannot be a recommended solution, as 777 is so insecure.
Is there any way I can resolve this over FTP without resorting to 777?

Can you not change the parameters on the open to try to simply open in Read Only mode? PHP Manual for fopen() options.
fopen("csv/newsletter.csv", "r");
This uses the least permissions, but you do need to have read access. Of course, you cannot write to the file. Also, 777 includes executable bit, so you might just want 666.
You could also try connecting via the FTP as a user, and ensure that users permission to the file is rw (6) and then others, do not need that setting so they could be read only.
Optionally through your FTP interface, maybe chmod() the file to be read/write before opening, then chmod read only after.

Related

PHP permissions denied failed to open stream

I have this code
<?php
$handle = fopen('names.rtf','w');
fwrite($handle, 'Alex');
?>
Warning: fopen(names.rtf): failed to open stream: Permission denied in
/Applications/XAMPP/xamppfiles/htdocs/file.php on line 3
And when I run it in comes up with that error. I've set my permissions on the folder to read & write for everyone. I'm not sure why else it would deny it.
Im using the latest Mac OS
There is possible, the issue is that your webserver or PHP instance is not running under the same user, you can check this using the get_current_user() function.
You can either configure apache to run with your privileges or give the folder more permissions, like 0777.but 0644 is enough for reading file. try to give permission according to it's usage.
Another thing, are you sure $outFile maps to an absolute path? Try doing:
var_dump(is_file("names.rtf"));

PHP File Uploading Issues

I'm trying to implement script that will allow a file to be uploaded, and then moved to a designated directory. This is running on a Windows server & IIS. I'm having 2 issues in doing so.
First, I get an error when trying to move the file.
Warning: move_uploaded_file(reports/ff.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\inetpub\wwwroot\betterinsight\betterinsight\upload_file.php on line 29
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\Temp\php1EAB.tmp' to 'reports/ff.jpg' in C:\inetpub\wwwroot\betterinsight\betterinsight\upload_file.php on line 29
Stored in: reports/ff.jpg
When searching, almost everything says it's a permission problem. I have added full rights (will pare down later) to the user: IIS_IUSRS. Even did it to the parent directory as one site recommended.
Second: As a possible solution to the above issue, I tried changing the directory in which files are saved. But the files are still uploaded into C:\windows\temp.
I've run phpinfo, and it says that uploads should be in (as defined by upload_tmp_dir):
Again, this is on a Windows Server 2008. Thanks.
Permission denied
You need to set the folder permissions to 777 or enable read write to the folder. I think the permissions are messing...
OK, the answer was simple. I was adding permissions for user: IIS_USRS, when it should have been just user: USRS.

FTP in PHP not copy index.php file

I have a PHP script to copy all files in a directory i have change my server now its not copying index.php file all other files copy successfully and throw bellow warnings.
Warning: ftp_get(tmp/readme.txt): failed to open stream: Permission denied in /var/www/vhosts/Site.com/httpdocs/includes/ftp.class.php on line 123
Warning: ftp_get(): Error opening tmp/readme.txt in /var/www/vhosts/Site.com/httpdocs/includes/ftp.class.php on line 123
Warning: fopen(tmp/readme.txt): failed to open stream: Permission denied in /var/www/vhosts/Site.com/httpdocs/includes/clone.php on line 196
Seems like a permission issue on the new server. Double check the server directory you're accessing to make sure, you and apache have the appropriate permissions.
Like the error message says: Permission denied, in other words your script does not have permission to access the file tmp/readme.txt. Check that the user your PHP script is using has read access to the file/folder. The way to check that depends on what your server setup is so can't give more unless you provide more info about what your server is running.
Welcome to the unix world. It is a permission problem. If you have rights, you can fix it with the command chmod in the server.
$ ssh username#server
$ chmod PERMISSIONS /path/to/filr
check the man page of chmod to understand the permissions. probably you do something like this : +w
It seems this permission issue, not in the new server but in the old server.
Please check owner:group of the index.php file in old server, so that your script can access and manipulate the origin file.
One more thing. Did you run the ftp transfer script, in the browser or in the command line?
This is very important, because one can run the script with APACHE user (www-data) and the other method run the script with your system user.

permission for my php to create a dynamic file on the server

For a project I need my php file to create a dynamic file (on remote server which I bought) which loads into a flash component. Everything is working fine on my localhost. But once I upload it to the server, it throws the following errors:
Warning: fopen(output.html) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 25
Warning: DOMDocument::save(k_id.xml) [domdocument.save]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 136
where on those lines fopen is written.
I understood as there is no permission to my php file to create any dynamic files on the server. So i just wanna understand is there a way by which I can privilege my php to create that file on the server.
I've the login access, which I think I've to put somewhere in code before it tries to create such file...but i dont know how to figure this...any suggestions?
The user that runs the PHP process needs to have permissions to write new files in the target folder. On linux servers this is done using CHMOD.
chmod 777 -R /path/to/folder
777 is the permission (full permission for testing only), -R means recursive for the files/folders inside. As you are using windows just right click on the folder and look at properties and search for permissions.
Create a new directory where the PHP will write the files. For that directory set permissions to 705 (that is owner has Read, Write, Execute permission)
You can either do that using a FTP client (Filezilla) or via a Bash shell
mkdir inputfiles
chmod 705 inputfiles

How to control the file permissions when the netbeans php plugin uploads a file with FTP

Whenever netbeans preforms an ftp upload the files are uploaded with the permissions set to 640. This causes PHP to throw the following error:
Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
Fatal error: Unknown: Failed opening required '/Library/WebServer/Documents/notes/index.php' (include_path='.:/usr/local/php5/lib/php') in Unknown on line 0
While I can clean it up with a quick chmod o+r ./* in the command prompt it causes netbeans to resynchronize the entire project next time I hit run which in turn sets the permissions back to 640.
I need a way to make netbeans upload the files with permissions set to 644.
Make sure the checkbox for "Preserve Remote File Permissions" under Project Properties > Run Configuration is checked, if you want to not change remote file permissions.
I determined that the problem was not with netbeans. It was with ftpd on the server.
The fix was to create a file called ftpd.conf in /etc/ and add the following line:
umask all 022
In your vsftp.conf file, look for this:
# Default umask for local users is 077. You may wish to change this to 022,
local_umask=022
Then, un-comment / adjust as needed.
Just to make a little easier if you are tired like I was. All the other answer where a rabbit hole and didn't work for me.

Categories