Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

SOLVED - PHP function fopen only can read a file

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Spark
Posts: 8
Joined: 2016-02-11 17:16

SOLVED - PHP function fopen only can read a file

#1 Post by Spark »

Hi everyone ...
I have recently installed Linux Debian 8.3 Jessie, with an Apache web server that works in localhost. I added PHP, MySQL and PhpMyAdmin.
Everything was fine till I started using some more specific functions.
For two purposes (generating PHP pages and creating txt files with the content of a MySQL table) I need to use the function fopen ... and it fails.
It might because I am making an obsolete use of that function, but it is probably linked to the rights that has that the server to write a file.

$fp = fopen ($fichier, "a");
if (! ($fp))
{
echo " -- Attention -- l'ouverture du fichier a échoué: vérifiez les droits d'écriture ...<br>";
return FALSE;
}
... gives that error message : Attention -- and so on.

Is there anything wrong with the server ? My use of this PHP function looks ok.
Last edited by Spark on 2016-02-16 20:04, edited 1 time in total.

Spark
Posts: 8
Joined: 2016-02-11 17:16

Re: PHP function fopen only can read a file

#2 Post by Spark »

I have given rights 777 to all PHP files involved in this.
Instead of option a for fopen I have also tried with w.
And it still does not work. There may be something wrong with the server.

Spark
Posts: 8
Joined: 2016-02-11 17:16

Re: PHP function fopen only can read a file

#3 Post by Spark »

Well ... I found the solution watching answers given in another forum.
I had to change the rights that has the user www-data ...
chown -R www-data:www-data /var/www/html/nujna

By the way ... I remembered I had to see what's up on file php.ini
I typed :
grep -i safe_mode /etc/php5/apache2/php.ini
and was surprised the only safe_mode variable is related to MySQL.
I actually do not understand what is that safe mode.
(Sometimes I feel unsafe about so much safety)

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: PHP function fopen only can read a file

#4 Post by dasein »

Spark wrote:I have given rights 777 to all PHP files involved in this.
You do not want to give the world write access to a script. Really.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: SOLVED - PHP function fopen only can read a file

#5 Post by GarryRicketson »

I use a PHP program on some websites, and the only way to edit some parts or files is to chmod them, then do the edit needed, but chmod them back to read only after
any editing or writeing is done. You should be able to modify the script so that
it does that as well. Also having it in a group and only that group can read/write,
in a nut shell there are ways to do this securly and safely.
Dasein> You do not want to give the world write access to a script. Really.
Is 100% correct on this.

The OP mentions this is on a "localhost" setup, so in one way
it probabley does not matter, but it is better to develope safer habits and practices, Definately do not upload this to a online server or hosting with those files
using

Code: Select all

chmod 777

When I first saw this post, there are a couple of reasons I did not respond,
1. I don't know that much about PHP and MYSQL,

2nd: reason: The OP does not use code boxes for the code:

These are code boxes:

Code: Select all

[code] (: any code :():can safely be (:written:)
[/code]
snd it will display correctly, just to demonstrate, look what happens with the above example , when I do not use code boxes:
(: any code :():can safely be (:written:)
As you can see the text shows a sad face instead of the actual symbols I used, any code or scripts may contain symbols or code that causes it to display poorly, but also
it makes it more difficult to read, and may not be showing exactly like the true code is.
Another reason, I did not respond,
gives that error message : Attention -- and so on.
Even though I do not know much about php or mysql, what little I do know, I have never seen a error message like that, if you want help, you need to really post exactly what the error message says.
Often and usually it refers to lines in the code that cause the error, that section or block of code is especially what we would need to see.

Later, just now I did translate it:
and in the code that was posted the error is in French, I did not have the time,nor desire
to have to translate it, but now I did:
opening the file failed: check writable
That by itself would have been enough to tell the OP, check and make sure
the file is writable, but since the OP did not bother to translate it, I did not bother
to try to answer.
The 3rd reason, the little piece of code the OP shows, is not enough to get the complete picture. Usually it is better to show the entire script, so we can see what it is doing. Any way, with all that said, please don't get me wrong, I am not trying to be rude
or snarky, I am trying to help you, and anyone else that takes the time to read, by explaining some things that will help get faster responses, and better answers in the
future. Please use code boxes for scripts and code, that is what they are here for.
Thank you
Last edited by GarryRicketson on 2016-02-17 03:00, edited 3 times in total.

User avatar
ralph.ronnquist
Posts: 342
Joined: 2015-12-19 01:07
Location: Melbourne, Australia
Been thanked: 6 times

Re: SOLVED - PHP function fopen only can read a file

#6 Post by ralph.ronnquist »

If Web Hacking would be Software Engineering, one would probably have both Version Control and separations between development workspace, pre-publishing staging area and live site area.

Spark
Posts: 8
Joined: 2016-02-11 17:16

Re: SOLVED - PHP function fopen only can read a file

#7 Post by Spark »

I have changed rights on these files to avoid that everyone can write on them.

Code: Select all

root@debian002:/cdpath# chmod -R 755 /var/www
... and this question keeps being solved : a user can launch, through some specific files, the creation of files :
- pictures renamed or redimensioned
- text files translating the contents of a table
- php files whose html source code is made according to the data base ...

Spark
Posts: 8
Joined: 2016-02-11 17:16

Re: SOLVED - PHP function fopen only can read a file

#8 Post by Spark »

ralph.ronnquist wrote:If Web Hacking would be Software Engineering, one would probably have both Version Control and separations between development workspace, pre-publishing staging area and live site area.
I am not sure I understood this.

I always separate a live site area and a localhost.
Only at the localhost are the files that can be modify the database or do whatever other task that could be targeted by a hacker.

Needstark
Posts: 2
Joined: 2016-04-20 21:46

Re: SOLVED - PHP function fopen only can read a file

#9 Post by Needstark »

after apache installed If you do not have permission to manipulate its contents, we give with the following. Change the directory owner and the group should use it. YOURUSER replace with the user name they are using

Code: Select all

chown -R YOURUSER:www-data /var/www
And next

Code: Select all

chmod -R 755 /var/www
And now you can test permiss with function is_readable() and is_writeable ()

Try this function

Code: Select all

<?php
$file-name = 'test.txt';
if (is_readable($file-name)) {
    echo 'Is readable';
} else {
    echo 'Is not readable';
}
?>
And other function but remplace is_readable () with is_writeable ()

Post Reply