Dador
Dador

Reputation: 5425

cURL upload files to remote server on MS Windows

When I use linux and try upload file to remote server using this script then all is well. But if i use Windows then script not working. Script:

$url="http://site.com/upload.php";
$post=array('image'=>'@'.getcwd().'images/image.jpg');
$this->ch=curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$body = curl_exec($this->ch);
echo $body; // << on Windows empty result

What am I doing wrong?

PHP 5.3

Windows 7 - not working, Ubuntu Linux 10.10 - working

Upvotes: 5

Views: 3350

Answers (5)

Aniruddh
Aniruddh

Reputation: 7668

I think, a better approach would be:

$imgpath = implode(DIRECTORY_SEPARATOR, array(getcwd(), 'images', 'image.jpg'));
$post = array('image'=>'@'.$imgpath);

Upvotes: 1

sanjeev
sanjeev

Reputation: 4631

if working with xampp Make sure that in php.ini configuration file

Line number 952 is uncommented i.e if line is

   ;extension=php_curl.dll

then make it

  extension=php_curl.dll

Upvotes: 1

Steffan Long
Steffan Long

Reputation: 337

You should try var_dump($body) to see what $body really contains. With the way you configured cURL, $body will contain either the response by the server or false, on failure. There is no way to differentiate between an empty response or false with echo. It's possible the request is going through just fine, and the server is just returning nothing.

However, as others have said, your file path seems invalid. getcwd() does not output a final / and you will need to add one to make the code work. Since you said it works on linux, even without the missing slash, I am wondering how it is finding your file.

I would suggest you create a path to the file relative to the PHP script that is running, or provide an absolute path and not rely on getcwd() which probably does not return what you are expecting. The value of getcwd() can be unpredictable across systems and is not very portable.

For example, if the file you are trying to POST resides in the same folder as your PHP script:

$post = array('image' => '@image.jpg'); is sufficient. If needed, provide an absolute path: $post = array('image' => '@/home/youruser/yourdomain/image.jpg');

As Terence said, if you need your code to be portable across Linux & Windows, consider using PHP's Predefined Constant DIRECTORY_SEPARATOR

$url = "http://yoursite.com/upload.php";
// images\image.jpg on Windows images/image.jpg on Linux
$post = array('image' => '@images'.DIRECTORY_SEPARATOR.'image.jpg');
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$body = curl_exec($this->ch);
var_dump($body);

getcwd() cURL

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

Theoretically, your code should not work(i mean upload) in any, unix or windows. Consider this portion from your code:

'image'=>'@'.getcwd().'images/image.jpg'

In windows getcwd() returns F:\Work\temp
In Linux It returns /root/work/temp

So, your above code will compile like below:

Windows: 'image'=>'@F:\Work\tempimages/image.jpg'
Linux: 'image'=>'@/root/work/tempimages/image.jpg'

Since you mentioned it worked for you in linux, which means /root/work/tempimages/image.jpg somehow existed in your filesystem.

My PHP version:
Linux: PHP 5.1.6
Windows: PHP 5.3.2

Upvotes: 4

Terence Eden
Terence Eden

Reputation: 14334

If you are using Windows, your file path separator will be \ not the Linux style /.

One obvious thing to try is

$post=array('image'=>'@'.getcwd().'images\image.jpg');

And see if that works.

If you want to make your script portable so it will work on with Windows or Linux, you can use PHP's predefined constant DIRECTORY_SEPARATOR

$post=array('image'=>'@'.getcwd().'images' . DIRECTORY_SEPARATOR .'image.jpg');

Upvotes: 4

Related Questions