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

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 and Working with Pydio (formally Ajaxplorer) in PHP - Part 2.

In this example we will be downloading single files rather than a zipped version of multiple files. To make this possible the file is temporarily created on the "server" before it is served to the user.

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

public function download ($dir)
{
    $urlDir = $this->prepareURL($dir);
    $arr = explode("/", $dir);

    //create a filename for it to be created locally in the same directory as this file is located
    $filename = dirname(__FILE__) . '/' . $arr[count($arr)-1];

    //create the file
    $fp = fopen ( $filename, 'w+');

    $curl = curl_init(trim(self::BASEURL . self::DOWNLOAD . $this->customer . $urlDir ));

    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data", "Ajxp-Force-Login: true"));
    curl_setopt($curl, CURLOPT_USERPWD,   self::UPWD );

    curl_exec($curl);
    curl_close($curl);
    fclose($fp); //close the file

    return $filename;
}

As can be seen above the file is created in the local directory of server. The full file path is then returned.

The section below is altered in the getFileList() JS function previously covered in part 1 of this tutorial.

JavaScript:

if(files[i]['is_file'] == 'true')
  {

      str+="<a href='data/FileHandler.php?method=downloadFile&client=" + client + "&path=" + files[i]['path'] + " class='list-group-item'><h4 class='list-group-item-heading'><i class='fa fa-file'></i> " + files[i]['name'] + "</h4><p>" + files[i]['modified'] + "</p></a>";
    }

NB: that the major change is the addition of the href value.

The section below is added to FileHandler.php.

PHP:

function downloadFile($dir, $folder){
    $pydio = new Pydio($folder);
    $dir = str_replace("/".$folder, "", $dir);

    if(substr($dir, 0,1) == "/")
        $dir = substr($dir, 0,strlen($dir));

    $filename = $pydio->download($dir);

    //open the file and get all the file details suchs as lenght, MIME type etc.
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 

    $mime = finfo_file($finfo, $filename); //get MIME type

    header('Content-Description: File Transfer');
    header('Content-Type:' . $mime);
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    readfile($filename);

    unlink($filename);
    exit;

}

Above we set the necessary headers to push the now downloaded file to the browser. This is done with the readfile() function.

Lastly the file is deleted. In a Linux system, such as the one this code is being run on, to delete a file simply remove it's IO Link with the unlink() function. The system will take care of the rest on clean-up.

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

Vanessa Coote

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