Link to home
Start Free TrialLog in
Avatar of BillyBoJimBob
BillyBoJimBob

asked on

Warning: mkdir() [function.mkdir]: File exists in ...

Hey y'all,

I am working on the following script with a wierd problem:
 - if a folder 'submitted_pictures' doesn't exist, it fails two to four times before it will succeed in creating the folder.
I am running this php script in a web browser, calling it from a unix web server, creating files on an intranet windows share.

I get the error: Warning: mkdir() [function.mkdir]: File exists in /var/www/html/custcard/custcard_folder_structure.php on line 42

1.  <?php
2.  
3.    // Remove the script time limit for this script
4.    set_time_limit(0);
5.  
6.    // Report ALL errors
7.    error_reporting(E_ALL);
8.  
9.    $start_dir = "/mnt/photo/NO_NAME_ROLLS";
10.   chdir($start_dir);
11.
12.   // Get all the NO_NAME_ROLLS directories
13.   $dirs = glob("*", GLOB_ONLYDIR);
14.
15.   $default_dirs = array('dealers','logs','pdf_output','thumbs');
16.   $bad_dirs = array();
17.   $mode = 0755;
18.
19.   if((is_array($dirs)) && (is_array($default_dirs))) {
20.     $sd = $start_dir.DIRECTORY_SEPARATOR;
21.     foreach($default_dirs as $de_dir)
22.       if(!in_array("'".$de_dir."'", $dirs))
23.         if(is_dir($sd.$de_dir) == false)
24.           mkdir($sd.$de_dir, $mode) or die($sd.$de_dir.' mkdir failed');
25.     foreach($dirs as $dir) {
26.       print_r($dir.'<br />');
27.       $strpath = $start_dir.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR;
28.       if(!in_array($dir,$default_dirs)) $bad_dirs[] = $dir;
29.       else {
30.         switch($dir) {
31.           case 'dealers' : {
32.             chdir($dir);
33.             $dealer_dirs = glob("*", GLOB_ONLYDIR);
34.             if(is_array($dealer_dirs)) {
35.               foreach($dealer_dirs as $deal) {
36.                 print_r($deal.'<br />');
37.                 chdir($deal);
38.                 $deal_subs = glob("*", GLOB_ONLYDIR);
39.                 if(is_array($deal_subs))
40.                   if(!in_array('submitted_pictures',$deal_subs))
41.                     if(!(is_dir($strpath."'".$deal."'".DIRECTORY_SEPARATOR.'submitted_pictures')))
42.                       (mkdir($strpath.$deal.DIRECTORY_SEPARATOR.'submitted_pictures', $mode))?print_r($strpath.$deal.DIRECTORY_SEPARATOR.'submitted_pictures mkdir succeeded<br />'):print_r($strpath.$deal.DIRECTORY_SEPARATOR.'submitted_pictures mkdir failed<br />');
43.                 chdir($strpath);
44.               }
45.               chdir($start_dir);
46.             }
47.             break;
48.           }
49.           case 'pdf_output' : {
50.             chdir($dir);
51.             $pdf_dirs = glob("*", GLOB_ONLYDIR);
52.             if(is_array($pdf_dirs))
53.               if(!in_array('printed',$pdf_dirs))
54.                 if(!is_dir($strpath.'printed')) mkdir($strpath.'printed', $mode)
55.                 or die('pdf_output/printed mkdir failed<br />');
56.             chdir($start_dir);
57.             break;
58.           }
59.           default : break;
60.         }
61.       }
62.     }
63.   }
64.   else foreach($default_dirs as $de_dir)
65.     if(!is_dir($start_dir.$de_dir))
66.       mkdir($start_dir.$de_dir, $mode) or die($start_dir.$de_dir.' mkdir failed');
67.     else echo $de_dir.' else folder created.<br />';
68.
69.
70.   if(count($bad_dirs) > 0) {
71.     echo '<br />The following directories are not default!!<br />';
72.     echo implode(', ',$bad_dirs);
73.   }
74.
75. ?>

Any ideas?

Thanks,
Bob.
Avatar of alextr2003fr
alextr2003fr

try to put @ to not show it :
42.                       (@mkdir($strpath.$deal.DIRECTORY_SEPARATOR.'submitted_pictures',
seems like it tries to overwrite already existing file/dir, maybe you should check that the file you are writing into does not exist yet? also check the directory levels on this line if(!(is_dir($strpath."'".$deal."'".DIRECTORY_SEPARATOR.'submitted_pictures')
Avatar of Marcus Bointon
As an alternative to PHP's mkdir function, you can use the system mkdir program with a -p parameter (see 'man mkdir', and calling system functions using system(), exec() etc). Though this is designed for making multiple levels of folders, it will work with a single level, and has the side effect of not generating errors if directories already exist. In PHP5 there's a recursive parameter for mkdir(), but I'm not sure if PHP5's implementation shares this feature - the docs don't say.
Avatar of BillyBoJimBob

ASKER

alextr2003fr:
>>try to put @ to not show it
This will keep the error from displaying, but when the error occurs, displayed or not, the folder is simply not created.
I need the folder to be created. period.

Squinky:
Calling system calls isn't supported by PHP in that you can't get full error capture, only the last line.  That last line is a string: in order to see if it failed or not, you have to parse the string.  This may fix the problem in a round about way, but it doesn't tell me what I did wrong with the script I have in place.


I basically want a clean way to figure out why the code I have works, but only after re-running the same code 2 to 5 times.

Bob.
I fixed this by bypassing the samba share problems and put this cron script on a windows server.  It works perfectly now.

Bob.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Squinky,

I will try that.  It would help efficiency to keep all my cron jobs on one server.

Thanks,
Bob.
All right folks... Sorry for the delay.

I need to find out if there is a bug with mkdir running on linux creating a directory on a samba share via php?
Running exec() or system() needs to be a last ditch option due to there will be times that I will have to have safe mode on and they won't  work when safe mode is on.

I need another viable option.

Thanks,
Bob.
Squinky,

While this solution: ( https://www.experts-exchange.com/questions/21387727/Warning-mkdir-function-mkdir-File-exists-in.html#13789243 ) may work on systems not running safe mode, I've been told it won't work when safe mode is on.  I am no longer serving the company this question was created for.  This solution works.  Working directly on the Windows share with a windows cron works better for our solution needs.

Since it does work, I'm giving you full kudos.

Thanks again,
Bob.