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);
}