0

I am creating a image upload functionality using php.

I was working on Windows using vagrant and everything worked fine. Now, I moved to Ubuntu (still using vagrant) and the script does not work anymore.

I create two folders, before I upload the image and just the first one is created at the moment.

$folder_name = Config::get('PATH_IMAGES') . Session::get('user_id') . '/';
$profile_folder = $folder_name . 'profile/';

// create a folder for the user's images
if (!(is_dir($folder_name) || is_writable($folder_name))) {
    mkdir($folder_name, 0777);
}           

if (!(is_dir($profile_folder) || is_writable($profile_folder))) {
    mkdir($profile_folder, 0777, true);
} 

This creates one folder with the following permissions:

drwxrwxr-x 2 user user 4096 Aug 9 21.33 folder_name

Not drwxrwxrwx!

Now, if I change the permissions manually (chmod -R 777 folder_name), I get the following:

drwxrwxrwx 2 user user 4096 Aug 9 21.33 folder_name

Running my script again, will create the second directory. Repeating this process with the second directory I am able to finally upload the image.

What I tried so far:

$folder_name = Config::get('PATH_IMAGES') . Session::get('user_id') . '/';
$profile_folder = $folder_name . 'profile/';

// create a folder for the user's images
if (!(is_dir($folder_name) || is_writable($folder_name))) {
    umask(0); <------
    mkdir($folder_name, 0777);
}           

if (!(is_dir($profile_folder) || is_writable($profile_folder))) {
    umask(0); <------
    mkdir($profile_folder, 0777, true);
}

This did not solve the problem. Running (umask();) returns 18.
Running chmod() after mkdir() did not help either.

My php user is www-data.
My apache user is 'user'.
There are no error messages.

I do not know where else to look for solutions. I would be very thankful for any help.

EDIT:

Putting it all in one statement does not solve the problem either.

    $folder_name = Config::get('PATH_IMAGES') . Session::get('user_id') . '/profile/';

    // create a folder for the user's images            
   if (!(is_dir($folder_name) || is_writable($folder_name))) {
        mkdir($folder_name, 0777, true);            
   }
Schwesi
  • 4,753
  • 8
  • 37
  • 62
  • Possible duplicate of http://stackoverflow.com/questions/25104990/mkdir-always-sets-folders-to-read-only/25105031#25105031 – Charlotte Dunois Aug 09 '15 at 19:54
  • @Chalotte Dunosis I tried using chmod() after mkdir() as well, but that did not change anything... – Schwesi Aug 09 '15 at 19:55
  • which are the permissions for the folder that contains folder_name? – Juan Girini Aug 09 '15 at 19:56
  • @Juancho Ramone Fully unrestricted (drwxrwxrwx 3 user user 4096 Aug 9 21.52 images) – Schwesi Aug 09 '15 at 19:58
  • Which PHP-Version are you using? – Charlotte Dunois Aug 09 '15 at 20:01
  • @Charlotte Dunois PHP Version 5.5.9-1ubuntu4.11 – Schwesi Aug 09 '15 at 20:03
  • Can't you create both folders in the same instruction by setting the recursive parameter to TRUE instead? – Juan Girini Aug 09 '15 at 20:04
  • the second part is necessary, because I reuse this function in my project. A couple of times, therefore now it is 'profile' (for profile images) somewhere else it will be 'post' (for images uploaded in posts) etc. – Schwesi Aug 09 '15 at 20:07
  • 1
    When you used `chmod()` did it return true? Check for that and if it does, check the folders permissions in FTP or SSH. – Charlotte Dunois Aug 09 '15 at 20:09
  • 1
    It looks like mkdir() sometimes does not like the trailing slashes https://bugs.php.net/bug.php?id=42739 I'd try without them – Juan Girini Aug 09 '15 at 20:10
  • @Juancho Ramone that sounds very promising, but how could I create a path without the slashes? (Sorry, I am really new to all this) – Schwesi Aug 09 '15 at 20:15
  • you only need to remove the last slash, change `$folder_name = Config::get('PATH_IMAGES') . Session::get('user_id') . '/'; $profile_folder = $folder_name . 'profile/';` to `$folder_name = Config::get('PATH_IMAGES') . Session::get('user_id'); $profile_folder = $folder_name . '/' . 'profile';` – Juan Girini Aug 09 '15 at 20:19
  • @JuanchoRamone That did not work, unfortunately. Same result. First folder, without unrestricted permissions. – Schwesi Aug 09 '15 at 20:22
  • @CharlotteDunois using ssh to check the permissions gives me the same result. I am not quite sure how to check if chmod returns true, but I do not get any error message running chmod. – Schwesi Aug 09 '15 at 20:23
  • 1
    @JuanchoRamone Safe mode was removed with PHP 5.4 – Charlotte Dunois Aug 09 '15 at 20:24
  • 1
    @kringeltorte Add after the first if clausel `die(var_dump(chmod($folder_name, 0777))); `, it will stop script execution and output the return of the chmod(). – Charlotte Dunois Aug 09 '15 at 20:27
  • @CharlotteDunois Thank you for the details! It returns bool(true). Folder permission is still restricted. – Schwesi Aug 09 '15 at 20:30
  • 1
    Can you try changing the PHP's user to the same user as Apache and then try again? – Charlotte Dunois Aug 09 '15 at 20:32
  • @CharlotteDunois I will try that! Might take a moment... – Schwesi Aug 09 '15 at 20:33
  • @CharlotteDunois Apache completely stopped working after changing the php user (Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}). Therefore, I guess I will have to reprovisioion and reinstall vagrant. I will get back to you as soon as I managed to achieve this. Thank you so much for all your help up until here!! – Schwesi Aug 09 '15 at 20:52

1 Answers1

0

use chmod($folder,0755) (or 0777) after mkdir()

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
CrsCaballero
  • 2,124
  • 1
  • 24
  • 31
  • Thank you! I tried that back then. I actually never found a solution for it, I think it was something very weird, which was not directly related to the code. – Schwesi Nov 25 '16 at 19:57