CodeIgniter Forums
mkdir and "file existing" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: mkdir and "file existing" (/thread-53332.html)



mkdir and "file existing" - El Forum - 07-19-2012

[eluser]Alhazred[/eluser]
I have to upload files from my application.
According to some form's fields filled by the user, the images must be put in different folders.

Before to upload the images I check if the path to the destination folder exists, if not I create all the missing folders.

The code is like this
Code:
//check if the $name folder exists, if not create it
if( ! is_dir(base_url('static/uploads/'.$name)))
{
if ( ! mkdir('./static/uploads/'.$name,0777))
  return FALSE;
}
  
//check if the $location folder exists, if not create it
if( ! is_dir(base_url('static/uploads/'.$name.'/'.$location)))
{
if ( ! mkdir('./static/uploads/'.$name.'/'.$location,0777))
  return FALSE;
}

return 'static/uploads/'.$name.'/'.$location;
The first time the folders are created, lets say the path is: /static/upload/John/Italy

If now John uploads other pics, he receives an error message like this
Code:
A PHP Error was encountered
Severity: Warning
Message: mkdir(): File exists
Filename: models/images_model.php
Line Number: 22
Where line 22 is the 1st mkdir().

is_dir() returns false even if the folder /static/upload/John exists, so the mkdir() is executed, but the folder is already there.

I've noticed that even if I set 0777 in the mkdir(), the created folders have 0755.
By the way, I've manually changed the permissions to 0777 (I'm on a local Linux machine) and the problem persists.

Is there anything wrong with my is_dir() checks?


mkdir and "file existing" - El Forum - 07-19-2012

[eluser]Unknown[/eluser]
You Just try this method:
//find folder path
$findFolderPath = getcwd().'/static/uploads/'.$name;
if(!file_exists($findFolderPath))
{
mkdir($findFolderPath, 0777);
}
//find folder path
$folderFilepath = getcwd().'/static/uploads/'.$name.'/'.$location';
if(!file_exists($folderFilepath))
{
mkdir($folderFilepath, 0777);
}


mkdir and "file existing" - El Forum - 07-19-2012

[eluser]Alhazred[/eluser]
Thanks for the reply.
I've found that removing base_url() my method works.
Maybe base_url() builds a bad path to pass to is_dir().


mkdir and "file existing" - El Forum - 07-19-2012

[eluser]CroNiX[/eluser]
base_url is a url, not a file path.


mkdir and "file existing" - El Forum - 07-19-2012

[eluser]Aken[/eluser]
base_url() generates an http://... URL. You need absolute or relative paths, not web URLs.