Jump to content

How to use Curl to upload an image from a custom module?


Recommended Posts

Hello, I'm trying to upload an image to a folder called uploads in my module's root. I have been able to accomplish this with the shop installed on my machine but I cannot upload the desired image to the remote shop. Here is my code:

 

connector.php, to use curl to send the image to the upload.php file:

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

if (isset($_FILES['userfile']['tmp_name'])) {
  $ch = curl_init();
  $cfile = new CURLFile($_FILES['userfile']['tmp_name'],$_FILES['userfile']['type'],$_FILES['userfile']['name']);
  $data = array('image'=>$cfile);

  curl_setopt($ch,CURLOPT_URL,_PS_BASE_URL_."/modules/customizeproducts/upload.php");
  curl_setopt($ch,CURLOPT_POST,true);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

  $resp = curl_exec($ch);

  if($resp == true){
    echo "success";
  }
  else{
    echo curl_error($ch);
  }
}

the upload.php file:

if (isset($_FILES['image']['tmp_name'])) {
  error_reporting(E_ALL);
  $uploaddir = 'uploads/';
  $uploadfile = $uploaddir . basename($_FILES['image']['name']);

  if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
      $data = array($uploadfile,$uploaddir);
      echo json_encode($data);
  } else {
      echo "Possible file upload attack!\n";
      print_r($_FILES);
  }
}

This is the console.log

 

"Possible file upload attack!
Array
(
    [image] => Array
        (
            [name] => shoex.jpeg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpkwpWFu
            [error] => 0
            => 8373
        )

)

"

 

I'm a newbie to php and curl

Help would be greatly appreciated!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...