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

Posted on 2005-04-13
Last Modified: 2012-06-21
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
3.    // Remove the script time limit for this script
4.    set_time_limit(0);
6.    // Report ALL errors
7.    error_reporting(E_ALL);
9.    $start_dir = "/mnt/photo/NO_NAME_ROLLS";
10.   chdir($start_dir);
12.   // Get all the NO_NAME_ROLLS directories
13.   $dirs = glob("*", GLOB_ONLYDIR);
15.   $default_dirs = array('dealers','logs','pdf_output','thumbs');
16.   $bad_dirs = array();
17.   $mode = 0755;
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 />';
70.   if(count($bad_dirs) > 0) {
71.     echo '<br />The following directories are not default!!<br />';
72.     echo implode(', ',$bad_dirs);
73.   }
75. ?>

Any ideas?

Question by:BillyBoJimBob
    LVL 6

    Expert Comment

    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')
    LVL 25

    Expert Comment

    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.
    LVL 1

    Author Comment

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

    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.

    LVL 1

    Author Comment

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

    LVL 25

    Accepted Solution

    > Calling system calls isn't supported by PHP in that you can't get full error capture, only the last line.

    That's not true. system() can return the full text of the output. exec() can return both the full text of the output (split by line into an array) and the result code. Both functions return their values via reference parameters.
    LVL 1

    Author Comment


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

    LVL 1

    Author Comment

    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.

    LVL 1

    Author Comment


    While this solution: ( ) 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,

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now