Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 412
Dador Avatar asked Jan 31 '11 00:01

Dador


2 Answers

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');
like image 64
Terence Eden Avatar answered Sep 24 '22 02:09

Terence Eden


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

like image 20
Sabuj Hassan Avatar answered Sep 25 '22 02:09

Sabuj Hassan