2017 © Pedro Peláez
 

library mjangiruploader

A simple library to easily handle file uploads, downloads and move with any PHP framework.

image

mjangir/mjangiruploader

A simple library to easily handle file uploads, downloads and move with any PHP framework.

  • Friday, October 16, 2015
  • by mjangir
  • Repository
  • 2 Watchers
  • 3 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

mkjuploader

A simple library to easily handle file uploads, downloads and move with any PHP framework., (*1)

Basically the library employs adapter concept to upload the files whether they are being uploaded in local directory or any third party storage. Currently I have only local adapter injected by default but I'm coming with other third party adapters like AmazonS3 etc., (*2)

How To Install Mkjuploader

$ composer require "mjangir/mkjuploader":"dev-master"

How To Use

The package can be used for various purposes I'm describing in the following., (*3)

1. Upload A File

A file can be uploaded following the below steps:, (*4)

  1. Create a suitable adapter having upload path and public path
  2. Create the main Upload object and pass the adapter in it
  3. Crate the File object for posted input
  4. Use the upload function of Uploader object
  5. It will return the actual uploaded file key

Refer the below code to work with immediately, (*5)

Create a database first, (*6)

CREATE DATABASE /*!32312 IF NOT EXISTS*/`mkjuploader` /*!40100 DEFAULT CHARACTER SET latin1 */;

/*Table structure for table `files` */

DROP TABLE IF EXISTS `files`;

CREATE TABLE `files` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `key` varchar(255) NOT NULL,
  `created` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

Write the PHP code index.php, (*7)

upload($fileObject);
    
    
    //Insert the file information in database
    $dbhost     = "DATABASE_HOST";
    $dbname     = "DATABASE_NAME";
    $dbuser     = "DATABASE_USER";
    $dbpass     = "DATABASE_PASS";

    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

    $sql = "INSERT INTO `files` (`key`,`created`) VALUES (:key,:created)";
    $q = $conn->prepare($sql);
    $q->execute(array(':key'      =>  $key,
                      ':created'  =>  date('Y-m-d H:i:s')
                     ));
}

?>

```html , (*8)




Get A File Info

You can get the complete file info on the basis of stored key in database. In the following example I'm using a static key for one of my stored files. <small>get.php</small> ```php <?php require "vendor/autoload.php"; use MkjUploader\Upload; use MkjUploader\Adapter\Local; //Get the key from the database. I'm using a static here $key = '6/4/3/643c4b13e88cb02e6e4a9fa6369666bbb83c978e/jdbc.zip'; //Create a local adapter. uploads folder must be there and writeable $adapter = new Local('uploads','uploads'); //Create main Upload object and pass the adapter $uploadObj = new Upload($adapter); //Get file info $info = $uploadObj->get($key); //Iterate over the object echo "Public Path :: ". $info->getPublicPath() ."<br/>"; //uploads/6/4/3/643c4b13e88cb02e6e4a9fa6369666bbb83c978e/jdbc.zip echo "Base Name :: ". $info->getBasename() ."<br/>"; //jdbc.zip echo "File Extension :: ". $info->getExtension() ."<br/>"; //zip echo "File Size :: ". $info->getContentLength() ."<br/>"; //2921919 echo "Mime Type :: ". $info->getContentType() ."<br/>"; //application/zip echo "Last Modified :: ". $info->getLastModified()->format('d M, Y') ."<br/>"; //16 Oct, 2015

Download A File Info

You can download a file on the basis of stored key in database. In the following example I'm using a static key for one of my stored files., (*9)

download.php, (*10)

<?php
require "vendor/autoload.php";

use MkjUploader\Upload;
use MkjUploader\Adapter\Local;

//Get the key from the database. I'm using a static here
$key = '6/4/3/643c4b13e88cb02e6e4a9fa6369666bbb83c978e/jdbc.zip';

//Create a local adapter. uploads folder must be there and writeable
$adapter = new Local('uploads','uploads');

//Create main Upload object and pass the adapter
$uploadObj = new Upload($adapter);

//Get file info
$info   = $uploadObj->get($key);

$fileName = $info->getBasename();

//If file name doesn't contain extension
if (!pathinfo($fileName, PATHINFO_EXTENSION) && $info->getExtension()) {
    $fileName .= '.'. $info->getExtension();
}

//Set the http response headers to download the file
$mimeType = $info->getContentType() ?: 'application/octet-stream';

header('Content-Type: "'.$mimeType.'"');
header('Content-Disposition: attachment; filename="'. str_replace('"', '\\"', $fileName) .'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".$info->getContentLength());

//Flush the content
echo $info->getContent();

Delete A File

A file can be simply delete by using the delete method of the adapter. Use the following code to delete a file:, (*11)

<?php
require "vendor/autoload.php";

use MkjUploader\Upload;
use MkjUploader\Adapter\Local;

//Get the key from the database. I'm using a static here
$key = '4/e/0/4e0fc90172f0fabcaca1ab042f1459b6587e440c/daterangepicker.png';

//Create a local adapter. uploads folder must be there and writeable
$adapter = new Local('uploads','uploads');

//Create main Upload object and pass the adapter
$uploadObj = new Upload($adapter);

//Delete The File
$delete  = $uploadObj->delete($key);

The Versions

16/10 2015

dev-master

9999999-dev

A simple library to easily handle file uploads, downloads and move with any PHP framework.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Manish Jangir