Downloading a PHP file (via Curl on the command line)

I have a nightmare using PHP to upload files to my server, however when using this simple HTML form it works:

<html><body> <form enctype="multipart/form-data" action="uploads.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> 

Then PHP:

 <?php $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> 

Is there any possible way to send a file using this method using curl from the command line or shell script? sort of:

 curl -f "@/path/to/my/file;type=text/html" http://mywebserver.com/uploads.php 

This particular line gives me: "curl: (22) Could not connect to :: 1: Permission denied", although there is no password on the site, etc. I assume this is possible, but I get the syntax wrong

+4
source share
1 answer

I think you want the argument to be capital F (for form). Line string F is for failure and returns error code 22. Seriously, this is in the manual!

-f, --fail

(HTTP) Failure (no output) for server errors. This is mainly for enabling scripts, etc. more efficiently, to better cope with failed attempts. In normal cases, when the HTTP server cannot execute the document, it returns an HTML document in which it is indicated (which often also describes why and much more). This flag will prevent curl from coming out of this and return error 22.

This method is not safe, and there are cases when unsuccessful response codes slip, especially when (response codes 401 and 407).

-F, --form

(HTTP) This allows curls to emulate a completed form in which the user has a submit button. This causes POST data to swirl using Content-Type multipart / form-data in accordance with RFC 2388. This allows binary downloads, etc. To force part of the content to be a file, the file name prefix is ​​@. Just get the content part from the file, the file name prefix with the symbol <. the difference between @ and <is that @ makes the file attached to the message as a file download, while <makes the text field and just gets the contents of this text field from the file.

In addition, you did not specify your file variable. I think you want:

 curl -F " uploadedfile=@ /path/to/my/file" http://mywebserver.com/uploads.php 
+7
source

All Articles