Working with Pydio (formally Ajaxplorer) in PHP - Part 4

If you've been in the market for open source cloud file sharing software, similar to Dropbox and Box, you may have come across Pydio(formally Ajaxplorer). It's an easy to install and use piece of software.

We continue to develop an already started example. In this post we will be looking at downloading files with the Pydio Server-Side API. For a review parts 1 & 2 of this tutorial, check out Working with Pydio (formally Ajaxplorer) in PHP - Part 1, Working with Pydio (formally Ajaxplorer) in PHP - Part 2 and Working with Pydio (formally Ajaxplorer) in PHP - Part 3.

In this post we will look at creating a new directory. Before I start this post, I will not that I am not following what the Pydio API suggest to make this call. Changes to the API that have not been documented, no longer require dir and dirname to be post parameters. This method in fact returns a false failure.

Let's add the PHP createDirectory function to the Pydio class we've already created.

public function makeDirectory($dir, $name){
    $urlDir = $this->prepareURL($dir);

    //replace the first slash with an empty space
    if(substr($urlDir, 0, 1) == "/")
        $urlDir = substr($urlDir, 1);

    $curl = curl_init(trim(self::BASEURL . $this->repository  . self::MAKEDIR .  $this->customer  . $urlDir . ($urlDir != '' ? "/" : "") . $this->prepareURL($name) ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data", "Ajxp-Force-Login: true"));
    curl_setopt($curl, CURLOPT_USERPWD,   self::UPWD );
        $response = curl_exec($curl);

    curl_close($curl);
    $xml = new SimpleXMLElement($response);
    $node = get_object_vars($xml->message);


    $state = array($node['@attributes']['type'] => dom_import_simplexml($xml)->textContent);

    return $state;
}

JavaScript:

var locStr = "<strong>Current Location: </strong>" + followPath +"<button class='btn btn-info pull-right' onclick='newDirName(1);'><i class='fa fa-plus'></i> Create Directory</button>"<br/><br/>";
$("#path").html(locStr);

The section above should replace the the string that displays the breadcrumbs in the getFileList() function.

function createDirectory()
  {
      <?php echo "var _client = '" . $_REQUEST["c"] . "';" ?>
      var _newName = $("#newName").val();
      var _repo = $("#nRepo").val();
      var _dirName = $("#nDirName").val();
      var _dir = $("#nDir").val();

      $.ajax({
          url: "data/ClientHandler.php",
          type: 'post',
          dataType: "json",
          data:{
              method: 'createDirectory',
              dir: _dir,
              newDir: _newName,
              client: _client,
              repo: _repo,
              dirName: _dirName
          },
          success: function(ret){
              if(ret == "duplicate")
              {
                  $("#createError").html("This directory already exists (names are case insensitive).");
                  $("#newName").val("");
                  $("#newName").focus();
                setTimeout(function(){$("#createError").html("");},5000);
              }
              else
              {
                  if(ret == "pass")
                  {
                      $("#newName").val("");
                    $("#newDir").modal("hide");
                      getFileList('', "CustomersPublic", "customerspublic", "out-path", "out");
                    getFileList('', "Customers", "customers", "path", "int");
                  }
                  else
                  {
                      $("#createError").html("An error occurred while attempting to create the directory.");
                  }
              }
          },
          error: function(err){
              alert(err.statusText + ' - ' + err.responseText);
          }
      });
  }
  function newDirName(val)
  {
      if(val == 1)
      {
          $("#nDir").val($("#path").attr('data-path'));
          $("#nRepo").val("Customers");
          $("#nDirName").val("customers");
      }
      else
      {
          $("#nDir").val($("#out-path").attr('data-path'));
          $("#nRepo").val("CustomersPublic");
          $("#nDirName").val("customerspublic");
      }
      $("#newDir").modal("show");
  }

We add 2 functions to the JavaScript. newDirName launches the modal seen in the HTML section. This examples works based on there being 2 possible repositories in which to create a folder. The createDirectory calls the PHP function also seen below.

PHP:

function createDirectory($dir, $newDir, $client, $repo, $dirName){
    $pydio = new Pydio($client, $repo, $dirName);
    $odir = $dir;
    $dir = str_replace("/".$client, "", $dir);

    $msg = $pydio->makeDirectory($dir, $newDir);

    if(array_key_exists("ERROR", $msg) && $msg["ERROR"] == "This directory already exists (names are case insensitive)")
    {
        return json_encode("duplicate");
    }
    else
    {
        if(array_key_exists("SUCCESS", $msg))
        {
            return json_encode("pass");
        }
        else
        {
            return json_encode("fail");
        }
    }
}

This example works for a multiple repository set-up which we have discussed creating in the first tutorial/post.

HTML:

<div class="modal fade" aria-hidden="true" role="dialog" tabindex="-1" id="newDir">
      <div class="modal-dialog modal-sm">
          <div class="modal-content">
              <div class="modal-header">
                  <h4 class="modal-title">New Directory Name</h4>
              </div>
              <div class="modal-body">
                  <div id="createError" class="text-danger"></div>
                  <div class="form-group">
                      <label>Directory name (must be unique)</label>
                      <input type="text" class="form-control" id="newName" />
                  </div>
                  <input type="hidden" id="nRepo" />
                  <input type="hidden" id="nDir" />
                  <input type="hidden" id="nDirName" />
              </div>
              <div class="modal-footer">
                  <button class="btn btn-default" data-dismiss="modal">Close</button>
                  <button class="btn btn-success" type="button" onclick="createDirectory();"><i class='fa fa-check'></i> Create</button>
              </div>
          </div>    
      </div>
  </div>

The HTML section above is the modal that appears for the user to enter the new directory name.

It is important to note that changes will need to be made to accomodate for changing repository names, as shown in parts 1 & 2. Also Pydio does not work with Repository names that contain a space or underscore. It may also be affected by other special characters.

Related Posts:
Working with Pydio (formally Ajaxplorer) in PHP - Part 1
Working with Pydio (formally Ajaxplorer) in PHP - Part 2
Working with Pydio (formally Ajaxplorer) in PHP - Part 3

Vanessa Coote

I'm a programmer and former lecturer with years experience working with PHP, ColdFusion, Java, Visual Basic, JavaScript, HTML, and CSS.