Html upload file to bash script

I am trying to upload a file to the server using bash script in html form.

<FORM NAME="FORM1" METHOD="post" enctype="multipart/form-data" ACTION="/cgi-bin/UPLOAD.sh">
  <INPUT type="file" name="DOCFILE" id="DOCFILE"
   accept=".jpg,.tif,.pdf">
</FORM>

How can I able to access the file in the script. I appreciate any example.

Link:
Linux wget command help and examples

This is a simple beginner's explanation of using wget in bash. Please read the WHOLE page. wget is meant to get files from the internet to your computer. IF you are at work and behind security (Example: a proxy or a nat server) things may get more complicated. See if this meets your needs.

You access the file in your form using a web server set up handle scripts with file extension .sh .

But frankly, no experienced web developer uses bash for processing HTTPD $_GET and $_POST requests (unless they have some tiny web site and are just playing around) because bash is not designed to handle HTTPD requests (like HTTP headers, cookies, and more) like other programming languages are.

If you are serious about learning web development, you should use a language build for processing HTTPD requests, like server-side Javascript, PHP, etc.

Web developers not use generally (or specifically) wget in HTTP form element action requests.

I have written 100s of HTML forms like the one above and the action is never wget.

wget is a command line tool, not a program run by a HTTPD request handler (a web server) .

OBTW, if you insist on using bash as your web server CGI, then you can check out this:

Simple CGI and Apache examples on Ubuntu Linux - LinuxConfig.org

FWIW, I disagree with the statement in the quote above " it still can find its place in Linux system administrator's hands as a quick tool for system monitoring and administration via web browser", because there is no place for using obsolete languages not designed for the web as a CGI when you have so many better choices (PHP, node.js, Python, etc).

I have been administering web servers "forever", and never once have I ever written or used a bash CGI like described in the link above. It's simply the wrong tool for the job.

BASH has no helpful features for interpreting CGI data. CGI gives you a few predfined variables, a raw data stream, and that's it.

Use this script and see what appears when you throw a file into it:

#!/bin/sh

# disable filename globbing
set -f

echo "Content-type: text/plain; charset=iso-8859-1"
echo

echo CGI/1.0 test script report:
echo

echo argc is $#. argv is "$*".
echo

echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = "$PATH_INFO"
echo PATH_TRANSLATED = "$PATH_TRANSLATED"
echo SCRIPT_NAME = "$SCRIPT_NAME"
echo QUERY_STRING = "$QUERY_STRING"
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo AUTH_TYPE = $AUTH_TYPE
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH
echo "Content begins:"
echo =====
dd count=1 bs=$CONTENT_LENGTH
echo
echo =====
echo "Content ends"

From this and the CGI standard you can reverse-engineer what's going on and build your own solution if you really really want to.

PHP on the other hand has native support for CGI data.

Thanks for the reply.

I will add little more information. I can read the web input from the bash script and I have no problem with form data like Name, Address, emal,...etc. When it comes to the file it seems I am only getting just the name.

DOCFILE="test.pdf"

Both these commands returned not true.

if [ -r "$DOCFILE" ] then ... fi
if [ -f "$DOCFILE" ] then ... fi

To repeat:

That means it doesn't create the file for you, or do anything else for you.

To repeat:

You have to create the file yourself. CGI feeds data into standard input and variables. Run the script to see what data you're given.

Why are you doing this? It this a homework problem?

The reason I ask, which we have pointed out to you before, is that bash is not designed to fully support HTTPD requests , natively; and so no serious web developer would be using bash as CGI. Even minor web-based tasks are better left to PHP, Python, node.js (server side javascript) etc.

So, if you are not doing homework, designed to teach you to do something using suboptimal tools and methods, why are you insisting to use the wrong tool for the job?