I want to make directory recursively by use of symlinks (softlinks) but I encouter with warning :
Warning: mkdir(): File exists in ...Path to php code... on line 21
The directory that i want to create is /vagrant/resources/page.
In the /var/www path I create a symlink named resources that links to /vagrant/resources directory and the php code is like below:
$directory = '/var/www/resources/page';
if(!file_exists($directory)){
mkdir($directory,0777,true);
}
The permissions to all directories inside /vagrant is set to 777.
Thanks.
As #arkascha mentioned your problem is not with symlinks but with the existence of the directory you are trying to make. which is a bit strange considering you have a fair condition around your mkdir command.
have try with is_dir() instead of file_exists()
I solved a similar problem with readlink. Also checking before if it is a link or not. After than the path can be used as expected.
$path = '/var/www/resources'
if (is_link($path)) {
$path = readlink($path);
}
Related
I'm trying to recursively create a directory using php's mkdir function in a Codeigniter installation. My code looks like this:
mkdir('docs/client/bills/payd', 0777, true)
The docs directory already exists in my site root dir, the client directory is beeing created with 0755 permission, the bills directory is beeing created with permission 1341 (weird!) and the last directory, payd, is never created.
I tryed to change permission in the mkdir argument list to 0, 755, etc... and nothing has changed. I also tryed to set umask to 0, 0777... and nothing.
umask(0777);
mkdir('docs/client/bills/payd', 0777, true)
Can anyone please say what am I doing wrong? The code above is called from a Codeigniter regular controller.
Try with
if ( ! is_dir( FCPATH.'docs/client/bills/payd' )//FCPATH is absolute path to the project directory
{
mkdir( FCPATH.'docs/client/bills/payd', 0777, true );//although 0755 is just fine and recomended for uploading and reading
}
Use this to specify it the working directory, it might be confused as to where the directory is located.
mkdir( getcwd().'docs/client/bills/payd', 0777, true);
getcwd is the working directory for your codeigniter. You can search in the PHP guide the getcwd() function to make it clearer.
This should work.
EDIT
To make it clearer, that would return the following:
C:\xampp\htdocs\YOUR_ROOT_DIRECTORY\docs\client\bills\payd
EDIT AGAIN
Although after reading again, this would only create payd and assume that docs\client\bills is already created. You could create client and bills using mkdir or using the file explorer. But there are other PHP ways and I can help if needed.
Goodluck meyt
I also had this weird "1341" permissions error with PHP mkdir, nothing to do with CodeIgniter, it's a pure PHP issue!
After much experimentation, the only way I could get it to work was to include a slash at the end of the path, and set the recursive flag to 'true'. (Even though the PHP docs don't show a final slash, and I was only creating a single directory.)
I.e.
$existing_path = '/these/directories/already/exist/';
mkdir( $existing_path . 'new-directory/', 0755, true);
I am getting the following php warning when using the scandir() function :
Scandir failed to open dir: opration not permitted in public_html/page2.php on line 3
page2.php
<?Php
$folder="/";
$result=scandir($folder);
print_r($result);
I want to use this function to print the files and sub directories of my root folder but it's not working.
Does someone know how to fix it?
Because you're trying to scan a directory that you don't have permissions to do (in this case the root directory /).
Either change the permissions, or scan a directory you do have permissions for.
This may either be on a directory permissions basis, or using the open_basedir directive.
The following works in windows:
mkdir('../my/folder/somewhere/on/the/server', 0777, true);
I am talking about PHP mkdir.
It works perfectly, and creates the subfolders recursively. However, if I run the same command on a linux server, the folders aren't created.
Previously I solved this by breaking up the path and creating each folder one by one. But I don't want to do that because it should work with the "resurive" flag set to true. Why isn't it working?
Sorry, but there must be some problem apart from the mkdir command itself.
This tiny example works as expected and recursively creates the directories for me when executed on Linux:
#!/usr/bin/php
<?php
mkdir ('testdir/testdir2/testdir3',0777,TRUE);
?>
This are the thing have discovered
Make sure the root path exists
Make sure the root path is writable
Don't use .. always use real path ...
Example
$fixedRoot = __DIR__;
$recusivePath = 'my/folder/somewhere/on/the/server';
if (is_writable($fixedRoot) && is_dir($fixedRoot)) {
mkdir($fixedRoot . DIRECTORY_SEPARATOR . $recusivePath, 0, true);
} else {
trigger_error("can write to that path");
}
Make sure that your PHP user (eg www-data) has permission to write to the parent folders of any folder it is trying to create. PHP needs to be able to write to the lowest one that already exists.
For example, in the case of ../my/folder/somewhere/on/the/server, if ../my already exists and PHP is able to write to .. but not to my, mkdir will fail.
If your user is www-data, you could use sudo chown -R www-data:www-data ../my to give write permission for my and all its subfolders.
Let's say, in PHP, I am trying to put an image in a specific directory that is on root.
I want to put it on /images/afc/esp/stadium/ directory. Images folder, Federation folder, Country ISO3 folder, content folder.
$folder_full = "images/".$getFed."/".$country_folder."/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full);
Before you ask, yes $getFed and $country_folder work and output text. So, then I get this error: Warning: mkdir(): No such file or directory
I don't get it?
Some of your subdirectories don't exist so you either need to iteratively create them or set the 3rd argument to mkdir() to true. Note that the second argument are the directory permissions (ignored on Windows) which default to 0777.
You also need to set $folder_full to the root using /.
$folder_full = "/images/{$getFed}/{$country_folder}/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full, 0777, true);
You need to use the recursive parameter to add directories that don't exist in the path you provide: mkdir($folder_full, 0777, true)
See the PHP docs here
All the intermediary directories have to already exist. You can trigger this behaviour by using the optional third argument:
mkdir($folder_full,0777,true);
in normal cPanel the folder permissions should be 0755
so the command in this case will be:
mkdir('tst/tst2/tst3', 0755, true);
My php script is located in /var/www/html/users/dev. I need to create a folder in /var/www/images/ - something like /var/www/images/test/test/ and store here some images.
But when i trying is with mkdir($file_dir, 0777); where $file_dir is /var/www/images/test/test/ i receive an error:
Warning: mkdir(): No such file or directory in /var/www/html/users/dev/classes/sites.class.php...
Because "/var/www/images/test" does not exists, so you can not mkdir("/var/www/images/test/test")
You can specify the "$recursive" to TRUE, and it will work, like this:
mkdir($file_dir, 0777, TRUE);
Try
mkdir($file_dir, 0777, true);
The third parameter ('recursive') allows you to specify a path of which all directories will be created. If you don't, only the last directory ('test') will be created, and the whole path before that must exist.
The PHP documentation is quite clear about that.
if it is Linux you have set permissions to your parent directory 1st.
sudo chmod -R 777 /path of ur directory.