LewlSauce
LewlSauce

Reputation: 5892

Use PHP to submit HTTP POST request containing image

So I've seen many examples of uploading an image via PHP using move_uploaded_file, but from the way that sounds, that would be a PHP script that resides on the server. In my case, I'm not trying to handle an uploaded file. I'm trying to submit a POST request and actually have the binary content of the file inserted with the HTTP POST request.

For example, my PHP script should be able to submit a form and include an image in its HTTP POST data, but I can't seem to figure this out or find valid examples specifically for doing this.

To further clarify, I am using CURL within PHP to submit this multipart/form-data.

Here's an example of what I have now:

function GetPostData($filename) {
    if(!$filename) {
        echo "The image doesn't exist ".$filename;
    } else {
        $data = [
            'device_timestamp' => time(), 
            'photo' => '@'.$filename
        ];
        return $data;
    }
}
function SendRequest($url, $post, $data, $userAgent, $cookies) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://i.example.com/api/v1/'.$url);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_PROXY, 'http://192.168.1.21:8080');
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    if($post) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    if($cookies) {
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');            
    } else {
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    }

    $response = curl_exec($ch);
    $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return [
        'code' => $http, 
        'response' => $response,
    ];
}

.

 $data = GetPostData($filename);
        $post = SendRequest('media/upload/', true, $data, $agent, true);    

But when I submit the image via the PHP script, this is what it looks like when I inspect the network traffic.

Content-Type: multipart/form-data; boundary=------------------------eee3f953c516cc55
Connection: close

--------------------------eee3f953c516cc55
Content-Disposition: form-data; name="device_timestamp"

1491023582
--------------------------eee3f953c516cc55
Content-Disposition: form-data; name="photo"

@/home/user/Desktop/square.jpeg
--------------------------eee3f953c516cc55--

Isn't the POST data supposed to contain the binary output of the image? How would the server save the image otherwise if just the path of the file is submitted in the form and not the actual image?

In other words, just like you would go to the terminal and type cat image.jpg, that's what I need PHP to submit in its form.

Upvotes: 0

Views: 714

Answers (2)

LewlSauce
LewlSauce

Reputation: 5892

Solved my own problem by changing

 function GetPostData($filename) {
        if(!$filename) {
            echo "The image doesn't exist ".$filename;
        } else {
            $data = [
                'device_timestamp' => time(), 
                'photo' => '@'.$filename
            ];
            return $data;
        }
    }

to this:

 function GetPostData($filename) {
        if(!$filename) {
            echo "The image doesn't exist ".$filename;
        } else {
            $data = [
                'device_timestamp' => time(), 
                'photo' => file_get_contents($filename)
            ];
            return $data;
        }
    }

Upvotes: 1

Ashish Shahi
Ashish Shahi

Reputation: 268

You have required to use in then you can easly uploded image

ex:-

<form action="" method="post" enctype="multipart/form-data">
</form>

PHP Code Required

 <?php 
    if(isset($_POST['submit']))
            {
            $filename=$_FILES['Yourfilename']['name'];
            $filetempname=$_FILES['Yourfilename']['tmp_name'];
            $fname=md5($_SERVER['REMOTE_ADDR'].rand()).$filename;
            $filepath1="uploads/folder name/".$fname;
            move_uploaded_file($filetempname,$filepath1);
    ?>

Upvotes: 0

Related Questions