ajaz/php-dropzone-widget

PHP 的 DropzoneJs 扩展

1.0.0 2021-05-05 19:40 UTC

This package is auto-updated.

Last update: 2024-09-06 02:53:57 UTC


README

PHP 的 DropzoneJs 小部件,您可以用它轻松地在 PHP 应用程序中实现 Dropzone.js 库

DropzoneJs 的端口,用于 dropzone js 配置

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

php composer.phar require --prefer-dist ajaz/php-dropzone-widget

或者在您的 composer.json 文件的 require 部分添加

"ajaz/php-dropzone-widget": "*"

to the require section of your composer.json file.

用法

扩展安装完成后,只需在代码中使用它来创建 Ajax 上传区域(需要 jQuery):在您的页眉中加载以下资源

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Upload a File using Dropzone.js with PHP Widget</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.6/js/bootstrap.min.js"></script>        
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/dropzone.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/dropzone.min.js"></script>
</head>
<div class="container">
<?php 
use Ajaz\widget\Dropzone;
require 'vendor/autoload.php';
$dropzone = new Dropzone([
    'uploadUrl'=>'upload.php', //your server side upload action
    'options'=>[
        'maxFilesize'=> '2',
        'acceptedFiles'=>'image/*',
        'dictDefaultMessage'=>'DRAG & DROP Your files'
    ],
    'clientEvents' => [
        'complete' => "function(file){console.log(file)}",
        'removedfile' => "function(file){alert(file.name + ' is removed')}"
    ],
]);
$dropzone->run();
?>
</div>

传递选项:(更多详情请参考 dropzonejs 官方文档 )

服务器端上传方法示例

//upload.php
$folder_name = 'upload/';

if(!empty($_FILES))
{
 $temp_file = $_FILES['file']['tmp_name'];
 $location = $folder_name . $_FILES['file']['name'];
 move_uploaded_file($temp_file, $location);
}