How to create folder in parent directory? - php

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.

Related

Error creating directory using mkdir function in Codeigniter

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

PHP mkdir permissions - wrong path

I have an problem and hope you can give me a hint for that:
I have an XAMPP running under OSx.
My APP is in htdocs/app.
Inside /app there is a folder /scripts and there is my PHP File:
app/scripts/file.php
Inside the /app folder there is a second folder /stuff that have 777 permissions.
from the script in app/scripts/file.php I want to create a folder
in app/stuff
But I got permission denied when I try to create a folder like:
mkdir('../scripts/newfolder', 0777, true);
What do I have to do?
Note:
When I test like this:
mkdir('newfolder', 0777, true);
Then it will work, but the new folder is under the wrong location:
app/scripts/newfolder
Give directory path as follows inside file.php.
mkdir('../stuff/newfolder', 0777, true);
If I understand you correctly you want to make a folder in app/stuff from the app/scripts folder, but you are telling the computer to go back into the apps folder (..), then back into the scripts folder (../scripts/) and then to make the newfolder.
mkdir('../stuff/newfolder', 0777, true);
should work then.
Your trying to create a directory from a dynamic place. You should prefer to use DIR which is the absolute directory of the current file being processed.
so you would use
mkdir(__DIR__ . '/../stuff/newfolder', 0777, true);
assume this line from your question is correct
from the script in app/scripts/file.php I want to create a folder in
app/stuff

php mkdir via symlink warning

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

mkdir fails in every way

Got this error: mkdir(): No such file or directory.
This is strange because ofcourse the directory doesn't exists because i want to create it. I checked the rights and its 0777.
The folder I tried to create is in: http://www.mysite/uploads/images/
so after folder creation it should look like: http://www.mysite/uploads/images/1
Anyone who can help me?
if (file_exists($upload_dir) == false)
{
mkdir($upload_dir, 0777);
}
mkdir function returns 'No such file or directory' in case there are not all parent directories exists. Please refer to third mkdir argument if you need recursive creation
mkdir($upload_dir, 0777, true);
Instead of given url path http://www.mysite/uploads/images/1 you need to given relative path of folder like
$upload_dir="/var/www/html/your_folder";// path of your folder
mkdir($upload_dir, 0777);
mkdir only works on The directory path

PHP create multiple directories

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

Categories