Web Analytics Made Easy -
StatCounter Creating subfolders on-demand - CodingForum


No announcement yet.

Creating subfolders on-demand

  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating subfolders on-demand

    I have a folder named data, in which I'd like to create sub-folders on demand.

    Below is the code I've put together but so far it won't create the sub-folders; anyone know why?

    NOTE: The session variables are defined.


    if (isset($_FILES['imgFile'])){

    if ( isset($_SESSION["Id"]) && isset($_SESSION["topicId"]) && isset($_SESSION["topicTitle"]) ){
    $newDir = "../data/" . $_SESSION["Id"] . "/topics/" . $_SESSION["topicId"] . "/";
    mkdir($newDir, 0777);
    $path = $newDir;

    Last edited by Dan06; Apr 1, 2009, 02:44 PM.

  • #2
    check the permission settings on the parent folder, also try giving exact paths (just so you know exactly where its trying to create the folder) and dropping the trailing slash


    • #3
      Originally posted by NancyJ View Post
      check the permission settings on the parent folder, also try giving exact paths (just so you know exactly where its trying to create the folder) and dropping the trailing slash
      I followed your suggestions and was able to create the sub-folders. However, when I try to move an uploaded file to that newly created sub-folder I get the following error:

      Warning: move_uploaded_file(D:/Apache/htdocs/tools/data/s789d5/topics/31/) [function.move-uploaded-file]: failed to open stream: No such file or directory in D:\Apache\htdocs\tools\php\files_upload.php on line 28

      Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\php86.tmp' to 'D:/Apache/htdocs/tools/data/s789d5/topics/31/' in D:\Apache\htdocs\tools\php\files_upload.php on line 28
      The directory definitely exists - I can see it in the system explorer. Why is it throwing this error? Below is my code:

      PHP Code:
      if ( isset($_SESSION["Id"]) && isset($_SESSION["topicsId"]) && isset($_SESSION["topicTitle"]) ){
      $newDir "D:/Apache/htdocs/tools/data/" $_SESSION["Id"] . "/topics/" $_SESSION["topicsId"];
      $path $newDir "/";
      } else {

      if ( 
      file_exists($path) ){    
      $targetPath $path basename($_FILES['imgFile']['name'][$i]);    

          for (
      $i 0$i count($_FILES['imgFile']); $i++){
              if ( 
      file_exists($_FILES['imgFile']['tmp_name'][$i]) ){
                  if ( 
      move_uploaded_file($_FILES['imgFile']['tmp_name'][$i], $targetPath) ){
                              } else {
      } else {
      "Directory does not exist: " $path;


      • #4
        $targetPath = $path . basename($_FILES['imgFile']['name'][$i]); is outside your loop.


        • #5
          Thanks for the help; that was a pretty silly mistake. Unfortunately, however, my problem in creating subfolders on-demand is not solved. There is a component to this problem that I did not mention. The files that are being uploaded are from a java applet.

          The intended process to creating sub-folders is as follows:

          A user fills out an html form, which stores the information in $_SESSION variables. Once the form is saved the user uploads files via a java applet. With the session variables set, once the web server receives the HTTP request, the php script creates sub-folders and processes the files via move_uploaded_file.

          What I figured out is that when the user fills out the form, the session variables are set BUT on the php script page when it tries to use the session variables to create the sub-folders - the session variables are empty.

          When I use a traditional html form to upload files, the php script works just fine - the session variables are defined and used.

          Anyone know why in spite of the session variables being set, the php script shows them to be unset when I upload files via my java applet?
          Last edited by Dan06; Apr 2, 2009, 12:16 PM.


          • #6
            Something you are probably doing already but you never know...make sure all of your pages that use sessions have session_start(); at the top of them.
            ||||If you are getting paid to do a job, don't ask for help on it!||||


            • #7
              The problem was the request submitted by the java applet - it was clearing the session variables. I solved the problem by passing the session variables to the applet which then submitted the parameters to the target php script.
              Last edited by Dan06; Apr 3, 2009, 02:49 PM.