I wanted to create a folder and set its permission.
It was working fine with windows, but when I shifted to linux,
it had permission problems.
//Make new directory
$directory = dirname($this->fileName);
if(!is_dir($directory)) {
if (!mkdir($directory, 0777, true))
die('Failed to create folders...');
} else {
die('ah ok...');
}
For this to work, I have to set the folder permission manually to 777.
Then after that the application can run successfully.
Then I delete the existing folder again to test it, cannot create again because
permission denied, it fails to create folder.
Check your umask() and set it to 0. If your umask is 0022 for example, your newly created directory will have permission 0755 instead of 0777 when you create it.
Related
I am working on a website that uploads images in sub dir, for example, every time the user uploads an image gets stored in storage/dropzone/upload/timestamp_date/image_name.jpg the problem I faced is the image is not showing up on the client side when I looked on my shared hosting I found that stores function to create a directory with no executable to world permission 700 and I wanted to be 755 or 777.
I tried to change the permissions of my shared hosting files but it works only once cuz when a new directory is created will create it with 700 permission
You can create a helper function for common use inside whole application.
use Illuminate\Support\Facades\File;
function checkFolderExists($folder, $permission)
{
if (!File::isDirectory(base_path('storage/' . $folder))) {
File::makeDirectory(base_path('storage/' . $folder), $permission , true, true);
}
return 'public/' . $folder;
}
where
$folder = 'dropzone/upload/timestamp_date'
$permission = 0777 or 0775 or your_choice
And after check the store folder and permission your file store path with store image in that specific folder :
$filePath = checkFolderExists('dropzone/upload/timestamp_date', 0775 or 0777 or your_choice);
$fileStoredPath = $request->file('form_name_field')->store($filePath);
I have a folder called 'folders'. I want to create a folder inside 'folders' called 'folder' with mkdir()
However the folder fails to create when 'folders' is set to 755. The only may I can get mkdir to work is making 'folders' 777. Is this typical or is there something else wrong? Shouldn't be able to do that on 755, isn't 777 a security risk?
A 777 permission on the directory means that everyone has access to read/write/execute (execute on a directory means that you can do a ls of the directory).
Try this one below if you fails to create folder inside the folder.
<?php
// Desired folder structure
$structure = './folders/folder/';
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0755, true)) {
die('Failed to create folders...');
}
?>
I am trying to make a directory when a new account is created.
The directory should be in my images folder and is used to better separate uploaded images.
//get the ID of the new account that was just created/inserted
$accountID = mysqli_insert_id($dbc);
//create a new directory path for that account
$directoryPath = "../images/" . $accountID;
// check if the directory exists
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
I had no problem getting this to work a few days ago, however when testing it today I am having problems.
I have included ini_set('display_errors', 'On'); in my page to see what error I am being thrown and it is a permission error.
Warning: mkdir(): Permission denied
The images folder has full read/write permissions for all users and groups as well as any parent folders so I don't understand how that would be an issue, that and it had worked several times before.
I am working on windows if that matters.
Any ideas?
To avoid spending too much time on permissions problems between the CLI user and the Apache user, an easy configuration is to use same user for both processes.
Get your user id and group by doing
$ id
uid=1000(my_user), gid=1000(my_group), ...
And then:
$ sudo service apache2 stop
$ sudo vi /etc/apache2/envvars
export APACHE_RUN_USER=my_user
export APACHE_RUN_GROUP=my_group
$ sudo chown -R my_user /var/lock/apache2
it's better and safer than to change you whole directory permission to 777
I think you should try this -
mkdir("../images/".$accountID, 0777, 'R');
Recursive folder creation may causing the problem.
Also get more information from - mkdir
Also check for folder permission.
You have to be sure that the parent directory allows you to create folder, and not the folder it self that is being created with 0777 rights...
Also, check with which user the Apache server is launched
Check if directory is already exist before using mkdir()
if (!is_dir ($directoryPath) ) {
mkdir($directoryPath, 0777);
}
I am working on windows if that matters.
It does.
Try changing this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
To this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath); //breaking here
}
You are on a Windows box so it will ignore the chmod mode. Also try using the full paths and not relative.
The mode is 0777 by default, which means the widest possible access.
For more information on modes, read the details on the chmod() page.
Note:mode is ignored on Windows.
http://php.net/manual/en/function.mkdir.php
I have folder hierarchy as Bucharest/Waterfall/a.php. My code snippet for making directory on server is as follows:
if(!is_dir($this->folder)){
$old_umask = umask(0);
mkdir($this->folder, 0777);
umask($old_umask);
}
chmod($this->folder, 0777);
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile))
return true;
All files are uploaded to the server. Now the issue is that the parent folder ,i.e. Bucharest has permission 755 while inner folder has permission 777. $this->folder has value as Bucharest/Waterfall. It gives 755 permission to Bucharest while 777 permission to waterfall. According to my code the 777 permission should also be given to Bucharest.
I have also tried chmod but all in vain. I want to give full permission to parent folder.
is_dir($this->folder); tells you that the file isn't a directory, not that it doesn't exist. and of course you can't mkdir if the directory is already there.
Also, if the directory already exists, according to the documentation chmod will not do anything if the user under which php is running isn't the same as the user who owns that directory.
Is the folder you are checking a subfolder already? If so, create the parent folders firts.
i.e.
mkdir('/tmp/test1/test2/test3/test4');
will fail if '/tmp/test1/test2/test3' doesn't exist
I have read this:-
Why can't PHP create a directory with 777 permissions?
and I can see a new folder being created by applying the following:-
// Desired folder structure
$structure = "../../../".$flash_dir."HELLO";
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
$oldmask = umask(0);
mkdir($structure, 0777);
umask($oldmask);
when viewing the file permission of HELLO with DreamWeaver, it is 777. However, I suspect it is a Linux 0777 rather than a Windows 777, therefore I still cannot upload things to HELLO.
Will there be any alternative method to create a directory with windows 777? Thanks!
PS. when I manual create a new directory and right click it to set 777, it works perfectly, so I really think it's related to Linux vs Windows~
0777 is exactly the same thing as 777
But I still can't say what the problem is. I would try to chmod it again after you've created it.
$oldmask = umask(0);
chmod($structure, 0777);
umask($oldmask);