1

I am having a peculiar issue with cURL on XAMPP 5.6.8. Using the below code, I am not able to send a file that does exist at the path specified in $tempPath. I am thinking that the cURL library may be getting confused with my path that starts with c:\.

My file is found here: C:\tempFolder\r_4878.tmp

On the linux server, using the exact same code, this does work using /mnt/temp/. Why should there be a difference?

What might be breaking here?

Upload Code

$post = array( 'file_name' => $reportID, 'file_contents'=>'@'.$tempPath.'' );

$return = true;

# set the url that we need to use to upload to each server
$url = "http://server.corp/uploadServer.php";

# curl the file to the remote server
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST,             true    );
curl_setopt( $ch, CURLOPT_HEADER,           false   );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,   true    );
curl_setopt( $ch, CURLOPT_TIMEOUT,          240     );
curl_setopt( $ch, CURLOPT_POSTFIELDS,       $post   );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json' ));

# get the servers respone and decode the json response
$result = json_decode( curl_exec( $ch ) );

$t = array( $url, $tempPath, file_exists( $tempPath ), $result->success, $post, $result );

echo "\r\n".print_r( $t, true )."\r\n\r\n";

curl_close( $ch );

Remote Server

$output['file_exists'] = file_exists( $_FILES["file_contents"]["tmp_name"] );
$output['file_path']   = $fileName.".txt";
$output['tmp_name']    = $_FILES["file_contents"]["tmp_name"];
$output['success']  = move_uploaded_file( $_FILES["file_contents"]["tmp_name"], $fileName.".txt" );

Response

Array
(
    [0] => http://server.corp/uploadServer.php
    [1] => C:\tempFolder\r_4878.tmp
    [2] => 1
    [3] => 
    [4] => Array
        (
            [file_name] => UnitTest25-felix
            [file_contents] => @C:\tempFolder\r_4878.tmp
        )

    [5] => stdClass Object
        (
            [file_name] => UnitTest25-felix
            [file_exists] => 
            [file_path] => UnitTest25-felix.txt
            [tmp_name] => 
            [success] => 
            [generationTime] => 9.70363616943E-5
        )

)
pcnate
  • 1,694
  • 1
  • 18
  • 34
  • Do a var_dump of $_FILES to see what the server is receiving if anything? – MiltoxBeyond Jun 23 '15 at 21:06
  • It's in the response – pcnate Jun 23 '15 at 21:51
  • That is your response not the actual value of the $_FILES array. Maybe it is sending the data as a different/temporary name. Or it may be sending it incorrectly. You can just do `$output['files'] = json_encode($_FILES);` – MiltoxBeyond Jun 23 '15 at 21:59
  • You will notice that the `tmp_name` is blank which is what is needed to access the contents. I checked and indeed the contents are not sent using curl on windows. Using cURL on Linux, this works perfectly fine from `/mnt/temp/` – pcnate Jun 23 '15 at 22:49

1 Answers1

2

I think you are only posting file information .. actual data is not posted. File data needs to be POSTed as multipart.

To debug you may want to create a form and see how that works in Network Tab . It will allow you see specifically how data is sent when using a browser.. Once you see that you will have accurate idea on how to POST the file data.

You should look at Using curl to upload POST data with files

Community
  • 1
  • 1
Scalable
  • 1,550
  • 4
  • 16
  • 29
  • Your client code [ where the problem lies], I-e the code making the curl call has nothing to do with apache. – Scalable Jun 24 '15 at 01:20
  • 1
    Except for being executed in php on apache/linux vs xampp/windows. I am not uploading with a browser – pcnate Jun 24 '15 at 01:21
  • 1
    Correct .. I am curious to see the actual request, as seen by the web server. As I said earlier it looks like you are only sending the file information. Actual data for file uploads is sent as a multipart request which I don't see in your curl call. Is curl is supposed to convert 'file_contents'=>'@'.$tempPath.'' to file data by reading it .. I don't think so. – Scalable Jun 24 '15 at 01:29
  • It does on the Linux server – pcnate Jun 24 '15 at 01:30
  • 2
    I see .. this is what you need CURLOPT_SAFE_UPLOAD . Depending on your php version .. the field may be set to true or false by default. You need to ensure it is true. – Scalable Jun 24 '15 at 01:49
  • Add this to your answer. I will test in the morning when I get to the office. – pcnate Jun 24 '15 at 03:11
  • This pointed me in the right direction. I set `CURLOPT_SAFE_UPLOAD` to false prior to specifying `CURLOPT_POSTFIELDS`. If you update the answer with this info, I choose it. – pcnate Jun 24 '15 at 11:32