mjangir/mjangiruploader
Composer 安装命令:
composer require mjangir/mjangiruploader
包简介
A simple library to easily handle file uploads, downloads and move with any PHP framework.
README 文档
README
A simple library to easily handle file uploads, downloads and move with any PHP framework.
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.
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.
1. Upload A File
A file can be uploaded following the below steps:- Create a suitable adapter having upload path and public path
- Create the main Upload object and pass the adapter in it
- Crate the File object for posted input
- Use the upload function of Uploader object
- It will return the actual uploaded file key
Refer the below code to work with immediately
Create a database first
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
<?php //Upload A New File Through $_FILE Input require "vendor/autoload.php"; use MkjUploader\File\Input; use MkjUploader\Upload; use MkjUploader\Adapter\Local; if(isset($_POST['submit'])) { //Create upload directory if not exist if(!is_dir('uploads')) { mkdir('uploads', 0777, true); } //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); //Create uploading file object $fileObject = new Input($_FILES['profile']); //Upload the actual file. It will give the uploaded key like below // 8/8/b/88bd070a290dba83f0594f791da5b8de8326c833/bootstrap-3.3.5-dist.zip $key = $uploadObj->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') )); } ?>
<!-- Upload File Form --> <form method="post" action="index.php" enctype="multipart/form-data"> <input type="file" name="profile"/> <input type="submit" name="submit" value="submit" /> </form>
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.get.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.download.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); $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:<?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);
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-15