Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with 'drag and drop' html5 uploader

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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)
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…
Suggested Courses

877 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