Christine Wilson
Christine Wilson

Reputation: 580

How to call UploadHandler.php with PHP - blueimp jQuery File Upload

Does anyone know how to upload images using PHP and calling the UploadHandler.php?

I'm not sure what information needs to be passed and in what format.

Here's what I have so far:

$prop="test";
session_id($prop);
@session_start();
$url = 'http://christinewilson.ca/wp-content/uploads/2013/02/port_rhdefence.png';
$file_name[] = file_get_contents($url);

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler(array(
    'user_dirs' => true
));

Upvotes: 4

Views: 16373

Answers (5)

Habib Mammadov
Habib Mammadov

Reputation: 321

First of all you should create protected variable:

protected $options;
protected $uploaded_files = [];

then you should assign to this variable the response value in post() method:

$this->uploaded_files = $response;
return $this->generate_response($response, $print_response);

then you should create public method which would return that response:

public function get_uploaded_files() {
        return $this->uploaded_files;
    }

and finally you should initiate the class and call the method:

$uploadPicture = new UploadHandler();
        $images = $uploadPicture->get_uploaded_files();

Hope this Helps !

Upvotes: 0

user1587439
user1587439

Reputation: 1651

The response is contained within the UploadHandler class object and can be retrieved like shown below.

$upload_handler = new UploadHandler();
$response = $upload_handler->response;
$files = $response['files'];
$file_count = count($files);
for ($c = 0; $c < $file_count; $c++) {
   if (isset($files[$c]->error))
       continue;
   $type = $files[$c]->type;
   $name = $files[$c]->name;
   $url = $files[$c]->url;
}

Upvotes: 4

Bradley Bossard
Bradley Bossard

Reputation: 2519

I ran into the same problem, where in the PHP I wanted to write all the URLS that UploadHandler.php had created to a mySQL database. If you look through the code, you'll see that

public function post($print_response = true)

actually returns the data structure from generate_response (which is a array with all the processed image metadata like image size, sanitized url, etc), but the call to $this->post() never does anything which it. So I add a variable

protected $upload_content = [];

to the class definition and changed the logic in function

protected function initialize()

to

        case 'POST':
             $this->upload_content = $this->post(false);
             break;

to update this variable after the images have been processed (you would need to do something similar with the GET case if you are using that). Then, I add a public function to the class to get this variable

 public function get_upload_content() {
     return $this->upload_content;
 }

and now the UploadHandler class can be called like this

 $upload_handler = new UploadHandler();
 $images = $upload_handler->get_upload_content();
 // Call a function that writes the urls in $images array to a database

Hope this helps!

Upvotes: 1

Kal
Kal

Reputation: 988

I could not find a way to get the file name via php so I had to do it myself.

First you need to add a public variable under UploadHandler.php

class UploadHandler
{
    public $file_name;
    protected $options;

and then add that to the function that creates the name

protected function get_file_name($name,
        $type = null, $index = null, $content_range = null) {

    $this->file_name = $this->get_unique_filename(
        $this->trim_file_name($name, $type, $index, $content_range),
        $type,
        $index,
        $content_range
    );
    return $this->file_name;
}

then under index.php you could do something like this

$upload_handler = new UploadHandler();
echo "\r\n [" . $upload_handler->fileName . "]\r\n";

I hope this help you or save someone some time :)

Upvotes: 3

Dirty-flow
Dirty-flow

Reputation: 2300

you can use the basic plugin:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
});
</script>
</body> 

Upvotes: 2

Related Questions