Search code examples
phplibcurlenctype

enctype curl php


I'm uploading an image to imagezilla.net using the following code

<form target="my_iframe" action="http://imagezilla.net/api.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" accept="image/x-png, image/gif, image/jpeg" />
    <input type="hidden" name="apikey" value="" />
    <input type="hidden" name="testmode" value="1" />
    <input type="submit" value="Upload Image" />
</form>

This works well but I don't have a way of getting the results back due to cross domain rules, so i'm trying to put it into a cUrl php

<?php
$ch = curl_init("http://imagezilla.net/api.php?file='C:\Anti-Backlash-Nut.jpg'&apikey=''&testmode=1");
$header = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);
echo $output;
?>  

(The second lot of code has the file already enclosed just as a quick test)
I can't get the Enctype in the php code to work properly (line $header = ...) as it just comes back as no file uploaded. What am I doing Wrong?


Solution

  • You are using GET method to upload the file ... but file always gets uploaded using POST.. For that you have to place @ in front of the file name you want to send as post.

    sending @ before the file path makes sure that cURL sends the file as part of a “multipart/form-data” post

    Try this

    <?php
     $ch = curl_init("http://imagezilla.net/api.php?apikey=''&testmode=1");
     $post = array(
        "file"=>"@C:\Anti-Backlash-Nut.jpg",
    );
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $output = curl_exec($ch);       
     curl_close($ch);
     echo $output;
    ?>