Search code examples
phpubuntupermissionswgetshell-exec

Wget Linux command is downloading files with Read Permissions for only sudo users - how to give Read permissions to all users?


I am running Ubuntu 16.04 and I am programming in PHP. Here is my problem:

I have a need to use the wget linux command to download all files from a website. So I am using PHP's shell_exec() function to execute the command:

$wgetCommand = "wget  --recursive  --page-requisites  --convert-links  $url  2>&1";
$wgetCommandOutput = shell_exec($wgetCommand);

This command downloads all files and folders from a website, recursively, to the current working directory (unless otherwise specified). The problem is that when these files and folders are downloaded, I do not have permissions to read them programmatically after that.

But if I do something like sudo chmod 777 -R path/to/downloaded/website/directory after they are downloaded with the wget command and before they are read programmatically, they are read just fine and everything works.

So I need a way to download folders and files from a website using wget command and they should have read permissions for all users, not just sudo. How can I achieve that?


Solution

  • This sounds like a umask issue with the user running the PHP script.

    Normally, Ubuntu would have a default umask of 0002. This would create a file with (-rw-rw-r--).

    From the console you can check and set the umask for the PHP user via:

    $umask
    

    And inside the PHP script, do a

    <?php
        umask()
    

    If you are on a running webserver, it would, however be better to alter the files permissions of the downloaded files afterwards, via

    <?php
        chmod()
    

    The reason is, that the umask handles file creation for all files - not just your script.