/    Sign up×
Community /Pin to ProfileBookmark

Drag and Drop file upload

DragAndDropIndex.php

[code]

<html>

<head>
<meta charset=”utf-8″>
<title>Drag and Drop Uploading</title>
<style>

body{
font-family: “Arial”, sans-serif;
}

.dropzone{
width:300px;
height:300px;
border: 2px dashed #ccc;
color:#ccc;
line-height: 300px;
text-align: center;
}

.dropzone.dragover{
border-color:#000;
color:#000;
}
</style

</head>

<body>
<div id = “upload”></div>
<div class =”dropzone” id=”dropzone”>Drop files to upload</div>

<script>
(function() {
var dropzone = document.getElementById(‘dropzone’);

var displayUploads = function(data){
var uploads = document.getElementById(‘uploads’),
anchor,
x;

for (x = 0; x < data.length; x = x + 1){
anchor = document.createElement(‘a’);
anchor.href = data[x].file;
anchor.innerText = data[x].name;

uploads.appendChild(anchor);
}
};

var upload = function(files){
var formData = new FormData(),
xhr = new XMLHttpRequest(),
x;

for(x = 0; x < files.length; x = x + 1){
formData.append(‘file[]’, files[x]);

}
xhr.onload = function(){
var data = JSON.parse(this.responseText);

displayUploads(data);
};

xhr.open(‘post’, ‘DragAndDropUpload.php’);
xhr.send(formData);
};

dropzone.ondrop = function(e){
e.preventDefault();
this.className = ‘dropzone’;
upload.(e.dataTransfer.files);
};

dropzone.ondragover = function(){
this.className = ‘dropzone dragover’;
return false;
};

dropzone.ondragleave = function(){
this.className = ‘dropzone’;
return false;
};
}());
</script>

</body>
</html>
[/code]

Hey guys Im creating a drag and drop file upload consisting of two pages, DragAndDropIndex and DragAndDropUpload. When I drag a file into the dropzone the file opens in the browser instead of uploading into the uploads folder. The ondragover/ondrag leave borders do not change when file goes over perhaps part of problem. Also would like to be able to be able to upload php and html files extensions if possible. I appreciate you guys.
DragAndDropUpload.php (below)

[code]
<?php
header(‘Content-Type: application/json’);

$uploaded = array();

if(!empty($FILES[‘file’][‘name’][0])){
foreach($FILES[‘file’][‘name’] as $position => $name) {
if(move_uploaded_file($_FILES[‘file’][‘temp_name’][$position], ‘uploads/’ . $name)){
$uploaded[] = array(
‘name’ =>$name,
‘file’ => ‘uploads/’ . $name
);
}

}
}

echo json_encode($uploaded);
[/code]

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@shakazorbaDec 01.2016 — dropzone.ondrop = function(e){

e.preventDefault();

this.className = 'dropzone';

upload[COLOR="#FF0000"][SIZE=4].[/SIZE][/COLOR](e.dataTransfer.files);

};
Copy linkTweet thisAlerts:
@rootDec 01.2016 — You have to have a <form> as part of the drag and drop.
Copy linkTweet thisAlerts:
@PeaceTime2323authorDec 02.2016 — Thanks guys. Still can't quite get the files into the upload directory but I am much closer now. I appreciate it
Copy linkTweet thisAlerts:
@PeaceTime2323authorDec 02.2016 — upload[COLOR="#FF0000"]s [/COLOR]directory, maybe thats my problem, can't spell tonight lol
Copy linkTweet thisAlerts:
@rootDec 02.2016 — Have you a <form> at all?

Have you set the enctype attribute correctly, the method as being POST ?

You didn't respond to my query, so I am assuming you haven't defined a form in HTML.

Also several things that you need to understand as far as PHP is concerned and the most important one is whether or not the server (hosting company) allow uploads via a web page and if the ability is turned on as well as the maximum size of the data upload as you find that they vary allot from as little as 1MB to a few hundred MB.

When uploads go to a server they are written to a temporary area that allows you to validate the upload for errors, file type, etc before moving the file to its final resting place.

If you look under the PHP forum, you will see a thread that contains the best example of file uploads script around.
Copy linkTweet thisAlerts:
@shakazorbaDec 02.2016 — for (x = 0; x < data.length; x = x + 1){

anchor = document.createElement('a');

anchor.href = data[x].file;

anchor.innerText = data[x].name;

[COLOR="#FF0000"]uploads[/COLOR].appendChild(anchor);
}


should have document.body or a reference to element to

contain links
Copy linkTweet thisAlerts:
@shakazorbaDec 02.2016 — here is the php code I used

php ...
[CODE]

<?php
header('Content-Type: application/json');
$uploaded = array();
for($i=0; $i<count($_FILES['file']['name']); $i++) {
$target_path = "uploads/";
$ext = explode('.', basename( $_FILES['file']['name'][$i]));
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
$uploaded[] = array(
'name' =>$target_path,
'file' => $target_path
);
} else{
echo "There was an error uploading the file, please try again! <br />";
}
}
echo json_encode($uploaded);
?>

[/CODE]
Copy linkTweet thisAlerts:
@rootDec 02.2016 — to get the extension of a file name in PHP [code=php]$ext = pathinfo($filename, PATHINFO_EXTENSION);[/code]

You still need a web <form> as PHP is so variable in its configuration, my host won't allow any uploads unless it is evident that a form object is present, even for drag and drop and my host is not the only one.
Copy linkTweet thisAlerts:
@PeaceTime2323authorDec 02.2016 — Thank you . and Shakazorba my partner and I are meeting this morning to discuss the solutions you have proposed. I am confident with the help you have provided that we will be able to continue to move forward on this project. Much appreciated!
Copy linkTweet thisAlerts:
@shakazorbaDec 02.2016 — I have put your code online here ...

http://handgunsforhomeless.com/upload/
×

Success!

Help @PeaceTime2323 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...