Menu

PHP Directory Structure Workaround

Help
Brian
2007-08-23
2012-09-21
  • Brian

    Brian - 2007-08-23

    Hello all,
    I'm fairly new to PHP, so there may, in fact, be a much better way of doing this, but it works for me rather well.

    ////file_upload.php source
    <html>
    <head>
    <title>Uploader</title>
    </head>

    <body>
    <applet name="JUpload" code="wjhk.jupload2.JUploadApplet" archive="wjhk.jupload.jar" width="700" height="500" mayscript>
    <!--param name="maxChunkSize" value="500000">
    <param name="uploadpolicy" value="PictureUploadPolicy"-->
    <param name="lookandfeel" value="system">
    <param name="nbFilesPerRequest" value="1">
    <param name="postURL" value="upload.php">
    Java 1.5 or higher plugin required.
    </applet>
    </body>
    </html>

    ////upload.php source
    //FORCE SOME PHP_INI SETTINGS
    ini_set( "max_execution_time", 120 );
    ini_set( "max_input_time", 240 );
    ini_set( "upload_max_filesize", "50M" ); //Maximum allowed size for uploaded files.
    ini_set( "post_max_size", "60M" ); //Maximum size of POST data that PHP will accept.
    ini_set( "memory_limit", "70M" ); //Maximum amount of memory a script may consume

    $save_path="files/";
    array_walk($_FILES, 'moveFiles');

    print_r($_FILES);
    print_r($_POST);

    //-----------------------------------------------------------------------------
    // FUNCTIONS
    //-----------------------------------------------------------------------------

    function moveFiles($name, $key) {
    global $save_path;
    $id = $FILES[$key];
    $tmpname = $_FILES[$key]['tmp_name'];
    $filename = stripslashes($_FILES[$key]['name']);
    $error = $_FILES[$key]['error'];
    if ($error == UPLOAD_ERR_OK) {
    $relpath = str_replace("\", "/", stripslashes($_POST["relpathinfo"][0]));
    $filename = $relpath . "/" . $filename;
    $filename = str_replace(" ", "
    ", $filename);
    echo $filename."\n";
    $pos = strrpos($filename, "/");
    if ($pos === false) {
    $file = $filename;
    $path = $save_path;
    } else {
    $file = substr($filename, $pos);
    $dir = substr($filename, 0, $pos);
    $path = $save_path . "/" . $dir;
    }

        if (@move_uploaded_file($tmpname, $path . &quot;/&quot; . $file)) { 
            echo &quot;SUCCESS\n&quot;; } 
        else { 
            mkdir_recursive($path);
            move_uploaded_file($tmpname, $path . &quot;/&quot; . $file);
            echo &quot;SUCCESS AFTER MAKING DIR\n&quot;;
            }
        }
    

    }

    // Creates the directories if needed.
    function mkdir_recursive($dir){

    do {
    $tempdir = $dir;
    while (!is_dir($tempdir)) {
    $basedir = dirname($tempdir);

         if ($basedir == '/' || is_dir($basedir)) {
            mkdir($tempdir,0777);
         } else {
            $tempdir=$basedir;
         }
      }
    

    } while ($tempdir != $dir);
    }


    This works by pulling the "relpathinfo" $_POST information for each file, and prepending it to the filename sent through the $_FILES array. Note that you need to have the nbFilesPerRequest set to 1 in the applet page, or else you'll end up with files all over the place, as I've not made the relpathinfo variable 'dynamic'. This is a quick and dirty fix, and I hope to get it working with the file chunks, but so far no luck with that.

    Any help or comments would be greatly appreciated!

     
    • Brian

      Brian - 2007-08-23

      Now working with chunked files!

      ////file_upload.php code
      <html>
      <head>
      <title>Uploader</title>
      </head>

      <body>
      <applet name="JUpload" code="wjhk.jupload2.JUploadApplet" archive="wjhk.jupload.jar" width="700" height="500" mayscript>
      <param name="maxChunkSize" value="500000">
      <param name="lookandfeel" value="system">
      <param name="nbFilesPerRequest" value="1">
      <param name="postURL" value="upload.php">
      Java 1.5 or higher plugin required.
      </applet>
      </body>
      </html>

      ////upload.php code
      //FORCE SOME PHP_INI SETTINGS
      ini_set( "max_execution_time", 120 );
      ini_set( "max_input_time", 240 );
      //ini_set( "upload_max_filesize", "50M" ); //Maximum allowed size for uploaded files.
      //ini_set( "post_max_size", "60M" ); //Maximum size of POST data that PHP will accept.
      //ini_set( "memory_limit", "70M" ); //Maximum amount of memory a script may consume

      $save_path="files/";
      array_walk($_FILES, 'moveFiles');

      //print_r($_FILES);
      //print_r($_POST);

      //-----------------------------------------------------------------------------
      // FUNCTIONS
      //-----------------------------------------------------------------------------

      function moveFiles($name, $key) {
      global $save_path;
      $id = $FILES[$key];
      $tmpname = $_FILES[$key]['tmp_name'];
      $filename = stripslashes($_FILES[$key]['name']);
      $error = $_FILES[$key]['error'];
      $size = $_FILES[$key]['size'];
      if ($error == UPLOAD_ERR_OK) {
      $relpath = str_replace("\", "/", stripslashes($_POST["relpathinfo"][0]));
      $filename = $relpath . "/" . $filename;
      //REMOVE SPACES FROM FILE & FOLDER NAMES, REPLACED WITH '
      '
      $filename = str_replace(" ", "_", $filename);
      echo $filename."\n";
      $pos = strrpos($filename, "/");
      if ($pos === false) {
      $file = $filename;
      $path = $save_path;
      } else {
      $file = substr($filename, $pos);
      $dir = substr($filename, 0, $pos);
      $path = $save_path . "/" . $dir;
      }
      //CREATE SUB-DIR IF NEEDED
      if (!file_exists($path)) { mkdir_recursive($path); }
      //CHECK FOR MULTI-PART FILE;
      if (isset($_POST["jupart"])) {
      //READ TEMP FILE DATA
      $tmpFileHandle = fopen($tmpname, 'r');
      $dataInsert = fread($tmpFileHandle, $size);
      fclose($tmpFileHandle);

              //APPEND DATA TO FILE IN SAVE PATH
              $createdFileName = $save_path . &quot;/&quot; . $filename;
              if (file_exists($createdFileName) &amp;&amp; $_POST[&quot;jupart&quot;] == &quot;1&quot;) { 
                  //TRUNCATES FILE IF ALREADY EXISTS WHEN BEGINNING UPLOAD OF SAME FILE NAME
                  $createdFileHandle = fopen($createdFileName, 'w');
                  fclose($createdFileHandle);
                  }
              $createdFileHandle = fopen($createdFileName, 'a');
              fwrite($createdFileHandle, $dataInsert);
              fclose($createdFileHandle);
          } else {
          //MOVE FILE TO SAVE PATH
              move_uploaded_file($tmpname, $path . &quot;/&quot; . $filename);
              echo &quot;SUCCESS\n&quot;;
              }
          }
      

      }

      // Creates the directories if needed.
      function mkdir_recursive($dir){

      do {
      $tempdir = $dir;
      while (!is_dir($tempdir)) {
      $basedir = dirname($tempdir);

           if ($basedir == '/' || is_dir($basedir)) {
              mkdir($tempdir,0777);
           } else {
              $tempdir=$basedir;
           }
        }
      

      } while ($tempdir != $dir);
      }

       
      • Brian

        Brian - 2007-08-23

        sorry all, I had a path typo; here's the fixed file.

        ////upload.php code
        //FORCE SOME PHP_INI SETTINGS
        ini_set( "max_execution_time", 120 );
        ini_set( "max_input_time", 240 );

        $save_path="files/";
        if (!file_exists($save_path)) { mkdir_recursive($save_path); }

        array_walk($_FILES, 'moveFiles');

        //-----------------------------------------------------------------------------
        // FUNCTIONS
        //-----------------------------------------------------------------------------

        function moveFiles($name, $key) {
        global $save_path;
        $id = $FILES[$key];
        $tmpname = $_FILES[$key]['tmp_name'];
        $filename = stripslashes($_FILES[$key]['name']);
        $error = $_FILES[$key]['error'];
        $size = $_FILES[$key]['size'];
        if ($error == UPLOAD_ERR_OK) {
        $relpath = str_replace("\", "/", stripslashes($_POST["relpathinfo"][0]));
        $filename = $relpath . "/" . $filename;
        //REMOVE SPACES FROM FILE & FOLDER NAMES, REPLACED WITH '
        '
        $filename = str_replace(" ", "_", $filename);
        echo $filename."\n";
        $pos = strrpos($filename, "/");
        if ($pos === false) {
        $file = $filename;
        $path = $save_path;
        } else {
        $file = substr($filename, $pos);
        $dir = substr($filename, 0, $pos);
        $path = $save_path . $dir;
        }
        //CREATE SUB-DIR IF NEEDED
        if (!file_exists($path)) { mkdir_recursive($path); }
        //CHECK FOR MULTI-PART FILE;
        if (isset($_POST["jupart"])) {
        //READ TEMP FILE DATA
        $tmpFileHandle = fopen($tmpname, 'r');
        $dataInsert = fread($tmpFileHandle, $size);
        fclose($tmpFileHandle);

                //APPEND DATA TO FILE IN SAVE PATH
                $createdFileName = $save_path . &quot;/&quot; . $filename;
                if (file_exists($createdFileName) &amp;&amp; $_POST[&quot;jupart&quot;] == &quot;1&quot;) { 
                    //TRUNCATES FILE IF ALREADY EXISTS WHEN BEGINNING UPLOAD OF SAME FILE NAME
                    $createdFileHandle = fopen($createdFileName, 'w');
                    fclose($createdFileHandle);
                    }
                $createdFileHandle = fopen($createdFileName, 'a');
                fwrite($createdFileHandle, $dataInsert);
                fclose($createdFileHandle);
            } else {
                move_uploaded_file($tmpname, $path . &quot;/&quot; . $file);
                }
            }
        

        }

        // Creates the directories if needed.
        function mkdir_recursive($dir){

        do {
        $tempdir = $dir;
        while (!is_dir($tempdir)) {
        $basedir = dirname($tempdir);

             if ($basedir == '/' || is_dir($basedir)) {
                mkdir($tempdir,0777);
             } else {
                $tempdir=$basedir;
             }
          }
        

        } while ($tempdir != $dir);
        }

         
    • Etienne

      Etienne - 2007-08-27

      Great!

      Thanks for your contribution.

      We'll try to embed it in the package, or at least add a link to this thread in the doc of the next release.
      (added on my todo list)

      Etienne

       
    • Libor Vanek

      Libor Vanek - 2007-09-03

      Hi,
      I'm sorry but it's not working to me - JUpload is simply not sending "relpathinfo" in the POST request (even if I got nbFilesPerRequest=1). What version are you using? I got 3.0.1.

      My "debug output" of send parameters by JUpload

      _FILES: File0 => Array
      name => yellow.gif
      type => image/gif
      tmp_name => /tmp/phpfe3iGG
      error => 0
      size => 1096
      _POST: mimetype => image/gif
      _POST: md5sum => 2caaa7e071620c5b686d8550335612e2

      I also "grepped" the JUpload source files (3.0.1) for the "relpath" string and no match :-(

      Thanks for any help,
      Libor

       
      • Brian

        Brian - 2007-09-03

        I'm using 3.1.0b1 [SVN-Rev: 309]

        I was having alot of problems with it at first, until I deleted the "temporary files" in the Java control panel. Even though my site was referencing the proper version of the uploader, Java had cached another version, and would pull that up instead.

         
        • Libor Vanek

          Libor Vanek - 2007-09-03

          Hi,
          can you please send me "compiled" version of it? I'm not Java developer and if I don't have to learn, how to compile Java on MacOS, I'd be very happy :-) My email is: libor.vanek [at] gmail.com

          Thx,
          Libor

           
          • Etienne

            Etienne - 2007-09-03

            Hi,

            The compiled version of the applet is available in each package available on sourceforge. Open the release jar file, and the /wwwroot/wjhk.jupload.jar file. It's the compiled applet.

            You can also browse SVN, and get the up to date applet (same path).

            Etienne

             
            • Libor Vanek

              Libor Vanek - 2007-09-03

              Ah, I didn't notice. Thx.

               

Log in to post a comment.