2

I want to make uploader script in CGI bash (shell script)

with a "browse" and "submit" to upload files from it

note : the uploader without ftp and without user and password of ftp or cpanel or sftp

or:

I want to make CGI script "bash", so script can upload files into the web.

saba
  • 35
  • 1
  • 10
  • You need to provide some more information. For example:-what have you tried so far? – Ankit Aug 31 '15 at 13:36
  • :/ my Question was clear -_- how we coded uploader the uploader was used to upload files on site and i want to coded the uploader in CGI shell script how can -_- – saba Aug 31 '15 at 13:45
  • See if this one helps - http://stackoverflow.com/questions/1894347/how-to-upload-ftp-files-to-server-in-a-bash-script – Ankit Aug 31 '15 at 13:49
  • not that helps , i want to make it in CGI and without ftp or sftp and whithout user and password , – saba Aug 31 '15 at 13:54
  • up please need help -_- – saba Sep 02 '15 at 11:03

1 Answers1

2

I did this using the example in Uploading Binary Files in Bash CGI Script but there's a small mistake which we fixed later:

file=/tmp/$$-$RANDOM

# CGI output must start with at least empty line (or headers)
printf '\r\n'
  cat <<EOF
<html>
<head>
<title>Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
EOF

IFS=$'\r'
read -r delim_line

IFS=''
delim_line="${delim_line}--"$'\r'

read -r line
filename=$(echo $line | sed 's/^.*filename=//' | sed 's/\"//g' | sed 's/.$//')
fileext=${filename##*.}

while read -r line; do
    test "$line" = '' && break
    test "$line" = $'\r' && break
done

# Note: This will result in junk at end of line (see format above)
cat > $file

# Get the line count
LINES=$(wc -l $file | cut -d ' ' -f 1)

# Remove the last line
head -$((LINES - 1)) $file >$file.1

# Copy eveything but the last line to a temp file
head -$((LINES - 2)) $file.1 >$file.2

# Copy the new last line but remove trailing \r\n
tail -1 $file.1 > $file.3
tail -c 2 $file.3 > $file.5
CRLF=$(hexdump -ve '/1 "%.2x"' $file.5)
# Check if the last two bytes are \r\n
if [ "$CRLF" = "0d0a" ];then
   BYTES=$(wc -c $file.3 | cut -d ' ' -f 1)
   truncate -s $((BYTES-2)) $file.3
fi

rm $file.5
cat $file.2 $file.3 > $file.4
cp $file.4 $file

cat <<EOF
<h1>Upload Successful</h1>
EOF

cat <<EOF
</body>
</html>
EOF

exit 0
parsley72
  • 8,449
  • 8
  • 65
  • 98
  • 1
    parsley72 very cool solution, thank you. i use your code in my website now. your solution is the only one i found in the whole internet that works for uploading binary files. – stefan-eibl Jul 03 '21 at 11:08
  • Thank you @parsley72. In my situation, the form data is starting with an additional input text field above the file itself. I had to add these lines after `read -r line` line, in order to gather the extra text: `while read -r line; do test "$line" = '' && break test "$line" = $'\r' && break done IFS=$'\r' read -r text_input_label read -r line IFS='' read -r line` – Jay jargot Apr 19 '22 at 10:11