13

I am trying to send a cURL request via the command line. Below is my request

curl -i http://localhost/test/index.php 
-X POST 
-F "name=file.png" 
-F "content=@/var/www/html/test/file.png"

The problem I'm having is that the file isn't getting sent with the request. The name is getting sent fine but not the file. Can anyone see if I am doing anything obviously wrong?

I've check the permission on the file as I thought that might be the problem but they are fine

The backend is written using the PHP Slim framework and I'm doing the following $app->request->post('content');

Serge S.
  • 4,855
  • 3
  • 42
  • 46
Bender
  • 581
  • 1
  • 6
  • 20

3 Answers3

9

If you want to be able to access the file's contents using $app->request->post('content');, your curl request must use a < instead of an @ for the content field,

curl -i http://localhost/test/index.php -X POST -F "name=file.png" \
  -F "content=</var/www/html/test/file.png"

From curl's manpage:

To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.

By using @, you marked the field as a file. Technically, this adds a Content-Disposition header to the field (RFC 2388, if you're interested). PHP detects that and automatically stores the field inside $_FILES instead of $_POST, as Raphaël Malié remarked in a comment, which is why you can't access the contents using $app->request->post.

You should consider to switch to using $_FILES in your backend, though, if you want to support uploading using browsers' <input type=file> elements and forms. Those always set a Content-Disposition header.

Phillip
  • 13,448
  • 29
  • 41
  • Thanks for your answer, I'll give this a try. Which method would you reccomend? Changing from @ to < and staying with POST or switching to `$_FILES`? – Bender May 13 '15 at 08:55
  • If you're uncertain, switch to `$_FILES`. ([There's documentation in case you need it](http://php.net/manual/en/features.file-upload).) It's standard to handle uploads that way, and while I can think of situations where you'd want to avoid it, they all are rather esoteric. – Phillip May 13 '15 at 09:01
0

There is a bug in php version 5.2.6 and lower. Verify your php version and test in latest. This might be the problem.

Sri Nair
  • 31
  • 6
0

@Bender, use the symbol @ but make the changes to your PHP Code, such that you make use of $_FILES variable. That will help. Make sure that you provide the Full Absolute path for the cURL. Else you will get the error.

Please refer the same kind of SO Question here: File Upload to Slim Framework using curl in php

Hope this helps.

Community
  • 1
  • 1
Vinod Tigadi
  • 859
  • 5
  • 12