user6475
user6475

Reputation: 33

Upload file with curl, follow redirect and download subsequent webpage

Apologies in advance if what I'm asking for is obvious or has been answered already somewhere else. I've tried to follow various guides and have now been struggling for a couple of hours, but didn't manage to get anywhere. Maybe you can help me?

I would like upload a file to a webpage with a PHP form (using POST), then follow the redirect the webpages gives and download the subsequent webpage/HTML file. As far as I understand, cURL is supposed to be used for these kind of jobs (as you can see, I'm totally new to this).

Here a test file that I would like to upload: hiddenlink

This is the webpage with the PHP form to which I would like to upload the test file: hiddenlink

You can manually upload the test file in the browser. The webpage will then redirect you to a new webpage with a large table. This website/HTML file can then be saved manually.

I would like to replicate this approach in the terminal, without having to use the browser.

This is the code I've been using so far:

curl -vL --form fileToUpload=@Test_Upload.txt --form press=submit *hiddenlink* -o savedpage.html

The idea is to upload the test file, then follow the redirect and save the new webpage as savedpage.html. This is not working out. Can you give me any directions on this? Again, my deepest apologies, but this is the first time I'm using cURL.

Upvotes: 1

Views: 500

Answers (1)

drew010
drew010

Reputation: 69947

The main issue I see is that you're posting to the wrong page for uploading.

The form itself, located at /upload/ is what you're posting to, but if you look at the form's HTML, the action is upload.php. This is where you should be directing your request. Note: curl doesn't read HTML so it can't figure out that by posting to the first URL, you want it to then send to the form defined in the HTML; you have to figure this and do it yourself.

curl -v -F [email protected] \
-F submit='Upload File' \
-o savedpage.html \
http://www.example.com/upload/upload.php

The -L isn't necessary because it doesn't appear that site sends any Location headers; still though, it doesn't hurt to have.

Upvotes: 1

Related Questions