Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with 'drag and drop' html5 uploader

Posted on 2011-09-08
3
Medium Priority
?
724 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 111

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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to count occurrences of each item in an array.
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)

661 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