Solved

Problem with 'drag and drop' html5 uploader

Posted on 2011-09-08
3
715 Views
Last Modified: 2012-05-12
Hi,

I found really nice drag and drop uploader which works perfectly.
Here's the link to tutorial: http://code.google.com/p/html5uploader
Link to demo: http://www.weeby.pl/blog/html5uploader/uploader.html
I also uploaded all the files needed.

Now, I want to store filenames of uploaded files in one variable (or one file per one variable is ok too), because I have this script in my form and after upload all the files needed, I click 'Send' button and I add the query to my mysql database, so it's processed by another php file, where I retrieve all fields values and save them to database ($_POST etc. - standard procedure for form processing).
So I need to modify the code a bit in uploader.php file I suppose to store all filenames in variable, so when I process the form, I can easily retrieve those names with $_POST.

I hope it's clear, please help, thanks.

You can find all the files in attached .zip, but here's the copy of uploader.php file:
<?php
$upload_folder = 'data';

if(count($_FILES)>0) {
        if( move_uploaded_file( $_FILES['upload']['tmp_name'] , $upload_folder.'/'.$_FILES['upload']['name'] ) ) {
                echo 'done';
        }
        exit();
} else if(isset($_GET['up'])) {
        if(isset($_GET['base64'])) {
                $content = base64_decode(file_get_contents('php://input'));
        } else {
                $content = file_get_contents('php://input');
        }

        $headers = getallheaders();
        $headers = array_change_key_case($headers, CASE_UPPER);

        if(file_put_contents($upload_folder.'/'.$headers['UP-FILENAME'], $content)) {
                echo 'done';
        }
        exit();
}
?>

Open in new window

dragdrop.zip
0
Comment
Question by:Zado
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36502016
If you have code you want to post for us here at EE, please post the code in clear text using the code snippet feature.  No offense meant, but I hope you understand our reluctance to open ZIP files from an unknown source.

File uploads are a very well understood pattern, and I can show you how to do that if you want.  But I do not understand the question.  Can you please rephrase this in the form of a question?  Thanks, ~Ray
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36502629
You can save it onto a session (but only if the move_uploaded_file() call succeeds:
<?php
//these should go to the very top of your page
if( !session_id() )
{
  session_start();
}


$upload_folder = 'data';

if(count($_FILES)>0) {
        if( move_uploaded_file( $_FILES['upload']['tmp_name'] , $upload_folder.'/'.$_FILES['upload']['name'] ) ) {
                echo 'done';
                if(!isset($_SESSION['uploadedFiles']))
                {
                   $_SESSION['uploadedFiles']=array();
                }
                $_SESSION['uploadedFiles'][]=$upload_folder.'/'.$_FILES['upload']['name'];
        }
        exit();
} else if(isset($_GET['up'])) {
        if(isset($_GET['base64'])) {
                $content = base64_decode(file_get_contents('php://input'));
        } else {
                $content = file_get_contents('php://input');
        }

        $headers = getallheaders();
        $headers = array_change_key_case($headers, CASE_UPPER);

        if(file_put_contents($upload_folder.'/'.$headers['UP-FILENAME'], $content)) {
                echo 'done';
        }
        exit();
}
?>

Since it is in a SESSION, then on the OTHER page, you just get it from that SESSION array - you don't need to add it on your "current" page as hidden fields:

<?php
//otherPage.php
session_start();

foreach( $_SESSION['uploadedFiles'] as $filename )
{
  //if you use basename, it will NOT include the full path to the image
  echo basename($filename);
  //WITHOUT basename, $filename will give you the fullpath to the image.
}

?>

Open in new window

0
 
LVL 8

Author Closing Comment

by:Zado
ID: 36503633
Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now