Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Testing a php file for school with fopen() function, which does not work because of... permission issues?

Writer Andrew Mclaughlin

I'm using Ubuntu in my laptop. Every time I turn it on, and after Ubuntu initializes, it ask me for my admin password, wich I do provide, in order to be able to use it. I've installed Sublime2, and I'm learning my first steps in PHP/MySQL I'm using this ubuntu laptop at my programming class. Today, we needed to do some fopen() tests for the first time.

I created a file and added to it this code:

error_reporting(E_ALL ^ E_NOTICE);
$fp=fopen("prueba.txt",'w');
fwrite($fp, 'Curso de PHP');
fclose($fp);
echo 'Horray';

Now, when I try to run the file (I'm using Xampp as my local apache, and I'm using chrome as my browser), I getthis error (and the file is not created at all):

Warning: fopen(prueba.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/curso_php1/inicial/ejemplo-archivos.php on line 25

Warning: fwrite() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/curso_php1/inicial/ejemplo-archivos.php on line 26

Warning: fclose() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/curso_php1/inicial/ejemplo-archivos.php on line 27

When at my home, I've tried the code again, in my windows macine, and everything worked as expected, without any errors.

It seems as I don have enough permissions (failed to open stream: Permission denied), why is that?

Why is this happening? What should I do? Thanks!"

2 Answers

The user running XAMPP apparently has no rights to write to /opt/lampp/htdocs/curso_php1/inicial/. I'd try writing to another file in a location with more permissive rights, like /tmp/.

Also, if you are not required to use XAMPP but just need a working Apache, try using the package Ubuntu provides. In my experience, the little gain in comfort due to not having to configure Apache and PHP yourself isn't worth the trouble with permissions and updates in the long run.

1

I've found that when this happens, the commands are these (I'm posting it here just in case anyone else happens to step into the same issue):

sudo chown -R myUserName /opt/lampp
sudo chmod -R 777 /opt/lampp 

I know that giving permissions 777 is not safe enough, but as I need this in my localhost, and I'm only using it for my programming classes, I think its ok.

Hope this helps someone, someday.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy