1

I am working on a project where I use two separate servers, one for the development and one for the visible version. This is how the process works, and where I'm having troubles: Every morning, I run some VBA macros that collects data, compiles that data (mainly .xlsx files) and sends it to my development server via FTP. The visible server is supposed to use that data to display some informations etc, but FTP is blocked on that server.

Because of that, I have to copy everything from my development server to the visible server every morning, so that the data on the visible server is updated, and I'd like to automatize that.

I tried sending the data from the VBA macros directly to the visible server via HTTP Requests (WinHTTPRequest to be exact) but that didn't work.

I searched online and found that cURL can send HTTP requests through PHP, and i'd like to use that solution if possible, here is my current code:

send.php:

<?php
    $request = curl_init('mysite/receive.php');
    curl_setopt($request, CURLOPT_POST, true);
    curl_setopt($request, 
    CURLOPT_POSTFIELDS, 
    array(
        'file' => '@MyFileRealPath.xlsx;filename=file'
    ));

    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    echo curl_exec($request);

    curl_close($request);
?>

receive.php:

<?php
    var_dump($_FILES);
?>

When I run send.php, I get:

array(0) { } 

So the receive.php file does not get any file, does someone know how to fix that?

If what I'm trying to do is not possible, does someone know any other way that I could try to send the files from the development server to the visible one?

Thanks and sorry for my not perfect english, I'm not a native speaker.

Have a nice day!

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Fawak
  • 33
  • 4
  • 1
    you could alternatively use `rsync` or `scp` to copy files from one machine to the other if possible – Alex Andrei Nov 05 '15 at 12:58
  • Check out [this](http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/) blog post. – cfreear Nov 05 '15 at 13:04
  • What version of PHP are you using? – Machavity Nov 05 '15 at 13:11
  • @Machavity I'm using PHP 5.6.10! – Fawak Nov 05 '15 at 13:17
  • Possible duplicate of [FB API PHP curl\_setopt\_array(): The usage of the @filename API for file uploading is deprecated](http://stackoverflow.com/questions/18482236/fb-api-php-curl-setopt-array-the-usage-of-the-filename-api-for-file-uploadin) – Machavity Nov 05 '15 at 13:19
  • Possible duplicate of [how to upload file using curl with php](http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – angelcool.net Nov 05 '15 at 17:34

1 Answers1

0

I had something similar today when our server suddenly did not send a file over curl anymore. I found that the PHP version was upgraded. Apparently, in a previous version a new curl setting (CURLOPT_SAFE_UPLOAD) was introduced which disables using @ symbol for file uploads, and "PHP 5.6.0 changes the default value to TRUE".

I think you need to add this setting using curl_setopt and disable it, like:

$request = curl_init('mysite/receive.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($request, 
CURLOPT_POSTFIELDS, 
array(
    'file' => '@MyFileRealPath.xlsx;filename=file'
));

curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

Alternatively, you can use CurlFile:

$request = curl_init('mysite/receive.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, 
CURLOPT_POSTFIELDS, 
array(
    'file' => new CurlFile('MyFileRealPath.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
));
Wouter M.
  • 61
  • 2