mjangir / mjangiruploader
一个简单的库,可以轻松处理文件上传、下载和移动,适用于任何PHP框架。
dev-master
2015-10-16 12:00 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 18:47:43 UTC
README
一个简单的库,可以轻松处理文件上传、下载和移动,适用于任何PHP框架。
基本上,该库使用适配器概念来上传文件,无论是上传到本地目录还是任何第三方存储。目前,我默认注入了本地适配器,但我还在开发其他第三方适配器,如AmazonS3等。
如何安装Mkjuploader
$ composer require "mjangir/mkjuploader":"dev-master"
如何使用
该软件包可用于以下描述的各种目的。
1. 上传文件
可以按照以下步骤上传文件- 创建一个合适的适配器,具有上传路径和公共路径
- 创建主Upload对象,并将适配器传递给它
- 创建用于上传输入的File对象
- 使用Uploader对象的upload函数
- 它将返回实际上传的文件键
请参考以下代码立即进行操作
首先创建数据库
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;
编写PHP代码 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.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.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方法简单删除文件。使用以下代码删除文件<?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);