cURL upload files to remote server on MS Windows cURL upload files to remote server on MS Windows curl curl

cURL upload files to remote server on MS Windows


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');


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


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