dropzone.js is a Open Source under MIT Licience library that lets you upload files (images, pdfs, text files, docs e.t.c) to the server via simple drag-and-drop option.
This tutorial provides an example with PHP :Steps :
Download the js dropzone-amd-module.js and css - dropzone.css from github : https://github.com/enyo/dropzone.
Create a HTML page that with file upload form :
Example: drag-drop-example.html<html>
<head>
<title>Drag drop Images, PDF, Docs and other files Example : PHP + Dropzone.js</title>
<script src="js/dropzone.js"></script>
<style type="text/css">
/*
* The MIT License
* Copyright (c) 2012 Matias Meno <m@tias.me>
*/
.dropzone,.dropzone * {
box-sizing: border-box;
}
.dropzone {
position: relative;
}
.dropzone .dz-preview {
position: relative;
display: inline-block;
width: 120px;
margin: 0.5em;
}
.dropzone .dz-preview .dz-progress {
display: block;
height: 15px;
border: 1px solid #aaa;
}
.dropzone .dz-preview .dz-progress .dz-upload {
display: block;
height: 100%;
width: 0;
background: green;
}
.dropzone .dz-preview .dz-error-message {
color: red;
display: none;
}
.dropzone .dz-preview.dz-error .dz-error-message,.dropzone .dz-preview.dz-error .dz-error-mark
{
display: block;
}
.dropzone .dz-preview.dz-success .dz-success-mark {
display: block;
}
.dropzone .dz-preview .dz-error-mark,.dropzone .dz-preview .dz-success-mark
{
position: absolute;
display: none;
left: 30px;
top: 30px;
width: 54px;
height: 58px;
left: 50%;
margin-left: -27px;
}
/* Custom decoration code */
.decoration {
border: 1px dashed #999;
background: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<div>
<form action="upload-files.php" class="dropzone decoration"
id="my-awesome-dropzone"></form>
<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 20, // Size in MB
addRemoveLinks: true,
removedfile: function(file) {
var fileRef;
return (fileRef = file.previewElement) != null ?
fileRef.parentNode.removeChild(file.previewElement) : void 0;
},
success: function(file, response) {
alert(response);
},
error: function(file, response) {
alert(response);
}
};
</script></div>
</body>
</html>
Create a PHP page to upload files to the server :
<?php
//PHP code to upload file to server directory
if (!empty($_FILES)) {
$temporaryFile = $_FILES['file']['tmp_name'];
$targetFile = "uploaded-file-dir-location". $_FILES['file']['name'];
if(!move_uploaded_file($temporaryFile,$targetFile)) {
echo "Error occurred while uploading the file to server!";
}
}
?>
More Posts related to PHP,
- Delete file using PHP code : unlink()
- PHP header location function not called
- 403 forbidden error for Image
- Call PHP function on Button click using jquery ajax
- How to Pretty Print JSON in PHP
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP drag and drop file upload tutorial using dropzone.js
- Upload Pdf file using PHP Script
- PHP Warning: Cannot modify header information - headers already sent
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- PHP Code for sending Emails
- How to destroy PHP session()
- Installing vue.js in Laravel 8
- PHP 301 Redirect Permanently
- Upload docx file using PHP script
- Save current timestamp in MySQL using PHP mysqi binding
- Copy file from one directory to other in Php
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Script to Upload Images to Server
More Posts:
- Docker Desktop needs privileged access macOS - MacOS
- How to Run all Cells at Once Jupyter Notebook - Python
- Encode or Decode Base64 String using Mac Terminal Command - MacOS
- Types of brackets used in Programming Languages - Codes - 2022
- Remove Applications from Startup Mac OS X - Mac-OS-X
- How to install SpaCy (NLP Library) on Mac - Python
- JSON Schema and Hyper-Schema : JSON Tutorial - Json-Tutorial
- Java 8 Predicate Chaining using and() or() and negate() Methods - Java