• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mkdir and "file existing"

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
//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
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?

You Just try this method:
//find folder path
$findFolderPath = getcwd().'/static/uploads/'.$name;
mkdir($findFolderPath, 0777);
//find folder path
$folderFilepath = getcwd().'/static/uploads/'.$name.'/'.$location';
mkdir($folderFilepath, 0777);

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().

base_url is a url, not a file path.

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.