Solved

Problem with 'drag and drop' html5 uploader

Posted on 2011-09-08
3
719 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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

685 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