Search code examples
phpapachefopenfile-ownership

Cant change owner of new file


I'm using PHP to retrieve some Base64 data, decode it and write it to a file, my code works fine. However, the owner is apache and the permissions are very low, so even if I FTP into the area where it's uploaded, i cannot download the file. However do i change the permission and owner so i have more control over the file? Here is my code

$path = "uploads/";
$filename = time().".png";
$full_path = $path.$filename;

$image_encoded = $_POST['image'];
$image_decoded = base64_decode($image_encoded);

$handle = fopen($full_path, 'x+');
fwrite($handle, $image_decoded);
fclose($handle);

Solution

  • The superuser and the files' owner can change the owner with:
    chown ( string $filename , mixed $user )

    The files' permissions can be changed using:
    chmod ( string $filename , int $mode )

    Plese note you need to use octal notation for the files' permissions, string notation as in Unix won't work! To grant the files' owner every permission, and to allow the other users to be able to read (download) it, the permisison in octal notation is: 0744

    See Wikipedia for more information about the octal notation of file permissions.