I'm running Lampp in Ubuntu, Working in /opt/lampp/htdocs/ folder.

The htdocs folder has write permissions enabled (I don't remember the exact command I ran to do that). I'm able to copy files from That folder to my home folder using cp/mv without any errors.

But I'm unable to do that using PHP. File.txt is placed in the htdocs folder and I want to move it to /home/username. I have tried using "copy" but it says "Failed to open stream" error and the Shell_exec / exec command won't show any error but doesn't copy the file either.

  • can you show the apache2/error.log? if there is a problem with your PHP-file it should be seen there – Mofty Nov 25 '15 at 8:30
  • It shows the Permission Denied error, But same command ran from terminal works fine without sudo. – Jappan Nov 25 '15 at 8:58
  • Or simply can you guide me to move my lamp folder somehow to home, So the user directly gets permission. – Jappan Nov 25 '15 at 9:01
  • that is not a good idea, cause the security would be really bad. i would make the folders r/w with chmod 755 *dir* – Mofty Nov 25 '15 at 9:07
  • Alright. I did that but still I'm unable to copy/move the file. – Jappan Nov 25 '15 at 10:22

The folder your PHP script is trying to write to will probably be owned by the root user. Your PHP script is running under the www-data user if you're using a default Ubuntu/Apache/PHP setup.

As such you need to:

chown -R www-data:www-data *dir*
chmod -R g+w *dir*

you don't have to do the second line if ls -al shows you this


If your PHP is running under a different user then just change the user in the first line of code.

that should fix your problem.

  • It worked, Daemon was the user running the php. – Jappan Nov 26 '15 at 8:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.