posting a file from command line with CURL

i needed to write a .bat script for windows to upload a log file. quick and nasty hack.. on the sending end:

curl -F "file=@%temp%\someFileName.log;filename=nameToBePresented.log" https://myserver/in.php
[code]

where in.php is:
[code]
$fName = "up/".date("Ymd_His")."_".uniqid();
$cnt = 0;
foreach($_FILES as $k=>$f)
  file_put_contents($fName.'_'.str_replace(['.','\\'],'',$f['name']).".".($cnt++).".body",file_get_contents($f['tmp_name']) );
echo 'ok';

looks straightforward, worked when i tested; but failed to upload in the production setup.

on the http server/PHP i was getting:

[Thu Feb 02 12:46:09.430384 2017] [:error] [pid 13273] [client 58.4.5.6:21549] PHP Warning:  file_get_contents(): Filename cannot be empty in /var/www/in.php on line 4
[Thu Feb 02 12:46:09.432869 2017] [core:info] [pid 13273] [client 58.4.5.6:21549] AH00564: Request header field is missing ':' separator: }
[Thu Feb 02 12:46:09.432875 2017] [core:info] [pid 13273] [client 58.4.5.6:21549] AH00567: request failed: error reading the headers

turned out that the log file was growing. curl at the beginning was sending header containing: Content-Length: 1234577 yet later the file was overgrowing that size.

instead of uploading the log file i’ve added one line to take a snapshot of it:

copy %temp%\someFileName.log %temp%\someFileName.log.snap
curl -F "file=@%temp%\someFileName.log.snap;filename=nameToBePresented.log" https://myserver/in.php

now it works!

Leave a Reply

Your email address will not be published. Required fields are marked *

(Spamcheck Enabled)