0

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);
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
dotty
  • 40,405
  • 66
  • 150
  • 195

2 Answers2

1

you might want to use chmod to change file permissions.

Perhaps you are not entitled to download it... You may change it's permissions to 755 or something...

chmod($full_path, 0755);
$handle = fopen($full_path, 'x+');
//...

http://www.php.net/manual/en/function.chmod.php

You should be able to change it's permissions because the file was created by php(the owner)...

Alex
  • 2,126
  • 3
  • 25
  • 47
1

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.

anroesti
  • 11,053
  • 3
  • 22
  • 33
  • All chown, chmod and chgrp (for the group) fail to do anything. – dotty Sep 30 '11 at 10:53
  • 1
    Actually, I'm wrong. Use 0755 instead of 755 worked in the chmod function. – dotty Sep 30 '11 at 10:58
  • For these functions to work, you need to be *superuser or owner of the file*. So, the user creating the file and the user changing the permissions need to be the same. Normally, both are `php`. If this isn't the case for you, something in your Apache configuration is probably broken. – anroesti Sep 30 '11 at 10:58
  • Please also read the second last paragraph in my answer, about the octal notation! Also, if you don't need to execute the file (as in your case), `0744` should be enough permissions! – anroesti Sep 30 '11 at 10:59