Can't upload image using curl

I want to upload images from my Server to another Server with php curl. I tested my script on localhost it works but on server it not working.

Here are my steps:

First step

I upload an image to my server. Path of the image is "imgs/myimage.jpg"

Second step

I excute this code for upload image from my server to another server.

$file_name_with_full_path = realpath("imgs/myimage.jpg");
$postimg = array('myfile'=>'@'.$file_name_with_full_path); 

$headers = array("Content-Type:multipart/form-data");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, "http://www.secondserver.com/image.php");    
$data = curl_exec($ch);        
curl_close($ch);

This code in image.php from second server.

print_r($_FILES["myfile"]);

I tested this code on localhost it works. But on Server it not working and display error.

Notice: Undefined index: myfile in ....

I try to get full path of the image on the server with command

echo realpath("imgs/myimage.jpg");

It shows this:

@/home/admin/domains/mydomain.com/public_html/member/imgs/myimage.jpg

I’m not sure what it is about Permission? But I have also set up file permissions are 777.

What should I do to fix this problem?

I’m trying to find a solution for the entire day, but was not successful.

You seem to create an array called $postimg but never use it, and use a variable called $postdata which you don’t show being initialised. Is that correct? Wouldn’t explain why it works on one server but not the other, though.

I’m Sorry. I put the wrong variable in my question. I tested it on a local it worked but on server it not worked.

Does this help at all? http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

I think it’s probably about my configuration server. Because my another server it works. It share host. But the server it does not work as a vps.

I would think you’re correct, it’s got to be a difference between your test server and the one it doesn’t work on. Perhaps try changing the destination php script to var_dump the whole of the $_FILES array, and perhaps $_POST at the same time, see if that gives any clues.

I have tried doing as you suggest. After excute the php script the command line var_dump($_FILES[“myfile”]) it show error this :
Notice: Undefined index: myfile in...
And the command line var_dump($_POST[“myfile”]) It does not show the error, but it could have recieve data from sent via post. But it is not correct. Because the value of the $_FILES[“myfile”] will be displayed. It was to Upload an image to the Server.

No, you already have a problem with that undefined index. I wanted to see what it does think is in the array.

var_dump($_POST);
var_dump($_FILES);

That just displays everything in those arrays, so you can see what the problem might be.

In var_dump($_POST); is
string(112) "@/home/admin/domains/mydomain.com/public_html/member/imgs/myimage.jpg"

In var_dump($_FILES); is

Notice: Undefined index: myfile in ... on line 6

Try removing the @ from the file name.

Edit:
http://stackoverflow.com/questions/17032990/can-anyone-give-me-an-example-for-phps-curlfile-class

1 Like

I tried this : http://stackoverflow.com/a/17033048/3563820

I got error message is

Warning: curl_setopt(): open_basedir restriction in effect. File() is not within the allowed path(s): (/home/admin/:/tmp:/var/tmp:/usr/local/lib/php/:/usr/local/php56/lib/php/:/usr/local/php54/lib/php/) in /home/admin/domains/roverpost.com/public_html/member/uploadimg.php

What were you trying to do when that message occurred, what is the line of code? Looks to me as if you’re trying to store the uploaded file in a directory you’re not allowed to access.

I tried this : http://stackoverflow.com/a/17033048/3563820

I’m working. Thank you @droopsnoot and @John_Betong for reply my question.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.