Solved

Problem with 'drag and drop' html5 uploader

Posted on 2011-09-08
3
720 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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