0

hi i have this code uploader.php:

class Uploader 
    {
        var $filePath;
        var $uploadURL;
        var $formFileVariableName;
        var $postParams = array ();

        function Uploader($filePath, $uploadURL, $formFileVariableName, $otherParams = false) 
        {
            $this->filePath = $filePath;
            $this->uploadURL = $uploadURL;
            if(is_array($otherParams) && $otherParams != false) 
            {
                foreach($otherParams as $fieldName => $fieldValue) 
                {
                    $this->postParams[$fieldName] = $fieldValue;
                }
            }
            $this->postParams[$formFileVariableName] = "@" . $filePath;
        }

        function UploadFile() 
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->uploadURL);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postParams);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $postResult = curl_exec($ch);

            if (curl_errno($ch)) 
            {
                print curl_error($ch);
                print "Unable to upload file.";
                exit();
            }
            curl_close($ch);

            return $postResult;
        }
    }

and i use like this:

require('uploader.php');

$upload_server = "http://developers.mytech.com.mx/files/api/upload.php";
     $file = $_FILES['archivo']['tmp_name'];
$archivo = $file;
$extension = pathinfo($archivo, PATHINFO_EXTENSION);
$nombre_base = basename($archivo, '.'.$extension);
    $upload = new Uploader($file, $upload_server,'file', array('api_key' => 'ic00n6yokd'));
    $result = $upload->UploadFile();
    if(preg_match("/upload_failed/", $result)){ echo "Upload failed."; }
    if(preg_match("/error_tamano/", $result)){ echo "Archivo muy grande."; }
    if(preg_match("/api_invalida/", $result)){ echo "Invalid API Code."; }
    if(preg_match("/error_type/", $result)){ echo "Archivo no valido."; }
    if(preg_match("/upload_success/", $result)){ echo $result; }

but the problem is that wht uploads is the tmp_name as a file and not the image but if i change this line: $file = $_FILES['archivo']['tmp_name']; to $file = $_FILES['archivo']['name'];

i get this error: failed creating formpost data

my question is how to use the class orectly so i can upload the image

1 Answer 1

0

maybe you should specify headers to your image data try this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'content-type: image/png'
 ));
Sign up to request clarification or add additional context in comments.

1 Comment

i tried moving file to the server and then when the file is already uploaded to the external server unlink the image from the server

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.