4

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
));
3
  • by default its set 'param_name' => 'files' in the class construct, so i guess it expects <input type="file" name="files" /> Commented May 14, 2013 at 20:57
  • Thanks I'm going to look into this more. When it sends "files" do you know what its actually sending? Is it the equivalent of file_get_contents in PHP? Commented May 18, 2013 at 14:56
  • No it will fill your $_FILES array with the path to the file and info. You access that in the same way as any other array e.g: $_FILES['files']. Check out php.net/manual/en/features.file-upload.post-method.php Commented May 18, 2013 at 16:49

5 Answers 5

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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 :)

Comments

2

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> 

3 Comments

This uses jquery to call UploadHandler class unless I'm missing something here. I'm looking to call the class with PHP instead.
As the name of the plugin says, you need jQuery to upload a file
Thanks Dirty-flow but I was looking for a way to not use jQuery and call the UploadHandler class with PHP only.
1

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!

Comments

0

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 !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.