Error creating directory using mkdir function in Codeigniter - php

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

Related

mkdir() not creating directory on web server even full path is specified

Please somebody help me, I want to create a folder 'uploads' in 'httpdocs' where my index.php file is placed, I am using Windows hosting with Plesk on Goddady. I went through available content on web but couldn't fix the issue, I am not very good with web servers. I have tried many solutions like full path specification, read - write permission, recursive directory creation using true/false etc but didn't work. It is working on my local server but not on web server. - Thanks in advance.
$path ="/PleskVhosts/abccat.in/httpdocs/uploads";
or
$path ="G:/PleskVhosts/abccat.in/httpdocs/uploads";
or
$path ="/httpdocs/uploads"; or $path ="/uploads";
mkdir($path, 0777, true);
I tried above all paths one by one, but didn't work. It is returning nothing as well. The full path for 'httpdocs' is G:/PleskVhosts/abccat.in/httpdocs.
Any help? Thanks.
PHP docs for mkdir() say that the mode is ignored on windows.
You may want to alter the permissions for the folders using chmod();
http://php.net/manual/en/function.mkdir.php
http://php.net/manual/en/function.chmod.php
Don't forget to set your permission back after you create your file.
chmod($folderPath, 0777); //<--This path would be for the folder you want your file in.
//You may have to do the chmod() for every folder all the way up to you target folder.
mkdir($filePath, 0777, true);
chmod($folderPath, 'mode'); //<---Put in the mode you need it to be. Do this for any folder you previously changed.
Try that and see if it helps.

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

Using PHP to create a new directory, and a file in that directory

I recently started to mess around with some HTML and PHP, and have run into what is probably a super easy to solve error, but one I have not for the life of me been able to fix.
In a nutshell, what I'm trying to do is create a directory, then create a .txt file in that directory with the same name, something to the effect of "/number/number.txt". While it seems simple to create a file, or to create a directory, I'm having no end of troubles trying to do both.
Here's my code:
mkdir($postnum, 0777);
chmod($postnum, 0777);
$post = "/" . $postnum . "/" . $postnum . ".txt";
$post = boards(__FILE__) . $post;
$po = fopen($post, 'w+') or die("Can't open file");
chmod($post, 0777);
A few issues I ran into writing this, I read that using mkdir I could set the permissions of the directory I create, but despite doing what I believe to be the right way of doing so, it didn't do anything. So I ran chmod right after.
Then, I had hoped that just /$postnum/$postnum.txt would work for the directory to open, but I get the die text when I try just that, I had to add in the "boards(FILE) part to get it to work. (Side note, "boards" is the directory I'm working in)
Even then, while it doesn't give me "Can't open file", it isn't creating the file or anything.
I've made certain that any file or folder even remotely interacting with the files has had it's permissions set to 0777, so it's likely not an issue with that. (Also, I know that having all my files completely open like that may not be the best idea; once I get this working properly, I'll be sure to set the permissions to something more safe)
Any idea what I'm doing wrong?
making directory and creating path:-
$new_folder=mkdir('uploads\\'.$folder_name, 0777, true);
('uploads\\'.$folder_name)->path where you create folder like www->upload->folder name(whatever you provide)
$path="uploads\\".$folder_name."\\";//if you want to provide path you can provide this way
The mkdir function takes a path relative to the root of the filesystem, not the document root.
If you're on a *nix system, your website document root is likely in a path such as /var/www/sitename/html, but you're trying to create /number/number.txt which your account doesn't have permissions to do. Change the path you pass into mkdir to somewhere in the filesystem you have permissions to create folders and files and your code should work.
Edit:
The code in your question doesn't actually attempt to write anything. By calling fopen you've created a file handle, but you need to call fwrite to actually write data to the file. As suggested by #JamesSwift, you may want to take a look at file_put_contents.
Here, try this :
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
mkdir("$root/root_website_folder/php/testing_folder", 0777);
PHP uses your real path not the virtual one, we assign to $root the real path on your machine than add the virtual path of your website root

MKDIR and CHMOD working , upload doesn't succeed

I have a really simple PHP script that creates a directory according to a product id, these folders are made to upload product id specific images into it.
Once this folder is made with PHP script mkdir('folder',0777) I upload an image with PHP to that just made folder. This doesn't work as it should : the move_uploaded_file function returns a regulation in the server safe_mode function. Although this the servers safe_modeproperty is turned off, is still gives this error / warning.
When I check with my FTP user account, I see the made directory with permission 777, but the uploads won't succees to upload to that directory...
Strangeness of it is that when i manually delete the made directory and make a new one (via FTP) the uploads work perfect!
Does anyone have any clue on fixing this issue? I'm not that server experienced :)
Thanks!
use this for mkdir username is your foldername in uplod folder
if(!is_dir('uploads/'.$username . "/")) {
mkdir('uploads/'.$username . "/", 0755);
}`
You have to take in account, that when creating a directory the create mask is anded with the umask:
$old = umask( 0 );
mkdir( 'folder', 0777, true );
umask( $old );
The mode on your directory may be being affected by your current umask. It will end
up having (mkdir-mode & (~umask)) permissions.
Try:
$oldmask = umask(0);
mkdir('folder', 0777);
umask($oldmask);

Create writable directories

For a project I'm implementing a file-upload system. For every user account I would like the script to create a different sub-folder. Lets say their user_id's.
Each time a user is added, the system will create a new sub-folder for their own uploads. For example:
Uploads/
- user1
- user2
- user3
By executing mkdir('Uploads/'.$user_id, 0777); it will create a new subfolder. Everything is fine.
However my application is not able to write to this folder. How do I have php make directories with the required file permissions? I have tried using chmod with no success.
This might help chmod and mkdir
$dirMode = 0777;
mkdir($directory, $dirMode, true);
// chmod the directory since it doesn't seem to work on recursive paths
chmod($directory, $dirMode);
For mkdir, mode is ignored on Windows. and 0777 is by default. and the third param is recursive which allows the creation of nested directories specified in the pathname.
sometimes the directory created with another mode than specified ( 0755 instead 0777 etc).
to solve that use :
<?php
$old = umask(0);
mkdir($dir,0777);
umask($old);
?>

Categories