Is there a simple way to use a text box in a form to check if a directory exists and if it doesn’t create it? If it does then store file through a upload field on same form. If it doesn’t create it and store files to it.
I’m using a mysql DB for storing data from form.

part of the Process.php file

$query = "INSERT into `".$db_table."` (Venue_Name,Gallery_Image_1,Gallery_Image_2,Gallery_Image_3,Gallery_Image_4,Gallery_Image_5,Gallery_Image_6,Gallery_Image_7,Gallery_Image_8,Gallery_Image_9,Gallery_Image_10) VALUES ('" . $_POST['Venue_Name'] . "','" . $_POST['Gallery_Image_1'] . "','" . $_POST['Gallery_Image_2'] . "','" . $_POST['Gallery_Image_3'] . "','" . $_POST['Gallery_Image_4'] . "','" . $_POST['Gallery_Image_5'] . "','" . $_POST['Gallery_Image_6'] . "','" . $_POST['Gallery_Image_7'] . "','" . $_POST['Gallery_Image_8'] . "','" . $_POST['Gallery_Image_9'] . "','" . $_POST['Gallery_Image_10'] . "')";
mysql_query($query);

I have another form that the Venue_Name is created from by user.

    a directory is just a file, so you can use file_exists() to check if it exists. off the top of my head I forget how to differentiate between a regular file and a directory, so you're best to check php.net on that.

      [man]is_dir/man to check if a directory exists and, if it doesn't, then [man]mkdir/man to make it.

      Also note that your code above is vulnerable to SQL injection attacks. User-supplied data should never be placed directly into a SQL query. Instead, it must first be sanitized with a function such as [man]mysql_real_escape_string/man (or else you can use a more updated library, such as MySQLi or PDO, to take advantage of prepared statements that do the escaping for you).

        Would this work with some mod and could someone help me incororate it if so.

        function handleError() {
            trigger_error('MY ERROR');
        
        /** usage sample
            @handleError();
            echo $php_errormsg;
        */
        }
        
        // detect slash/backslash nomenclature dirname
        $path = dirname( __FILE__ );
        $slash = '/'; strpos( $path, $slash ) ? '' : $slash = '\\';
        define( 'BASE_DIR', $path . $slash );
        
        $folder  = time();               // folder name
        $dirPath = BASE_DIR . $folder;   // folder path
        
        $rs = @mkdir( $dirPath, '0777' );
        @handleError();
        if( $rs )
        {
        
        // print success information
        echo 'was done!';
        echo '<br>folder: <a href="' . $folder . '">' . $folder . '</a>';
        echo '<br>dirPath: ' . $dirPath;
        
        }else{
        
        // print error information
        echo 'an error was occurred. Attempting create folder';
        echo '<br>dirPath: ' . $dirPath;
        echo '<br>php_errormsg: ' . $php_errormsg;
        
        }

        Instead of folder name time() use the database field Venue_Name from another table to create the directory under gallery/gallery_images which is a sub directory from where this form is located at. Which might be coming from BASE_DIR part of this? My thought is in the form/process of the gallery form the text field would be where the person enters the venue_name on submit it would process the data to mysql and this here code would make the dir if not exist and place the uploaded images in it if it did exist then upload to the dir.

          Can someone tell me what to set the BASE_DIR to to get the above code to make the new directory in a sub dir off of where this code file is in excample this code is in admin/gallery I need it to make the new dir in admin/gallery/gallery_images. So it would look something like admin/gallery/gallery_images/newdir. Ive tried every thing I could think of its probably another case of over think it but I need the help
          Thanks

            So the subdirectory would appear in "gallery_images", a folder that is within the same folder that the script is in? In that case, the relative path would be "gallery_images/$new_dir_name".

            Also, this bit of code is unnecessary (and doesn't even make sense):

            // detect slash/backslash nomenclature dirname
            $path = dirname( __FILE__ );
            $slash = '/'; strpos( $path, $slash ) ? '' : $slash = '\\';

            Get rid of the $slash stuff and use PHP's builtin PATH_SEPARATOR constant.

              No not really the script is in admin/gallery then gallery_images is in gallery so you got admin/gallery(script)/gallery_images/newdirs

              Huh😕 on rest of post

                Write a Reply...