巴什CGI上传文件 [英] Bash cgi upload file

查看:168
本文介绍了巴什CGI上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有这样的问题。我有HTML

Hello i have such problem. I have html

<form action="/cgi-bin/echo.cgi" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

和我需要编写从表单文件使用服务器的bash CGI(不使用Perl)。我尝试这样的事情

And i need to write this file from form to server using bash cgi(not perl). I try something like this

POST=$(</dev/stdin)
echo "$POST">"/tmp/a"

,但它不工作

P.S。我看到这个Apache日志 /var/www/cgi-bin/echo.cgi:6号线是:/ dev /标准输入:没有这样的设备或地址

P.S. i see this in apache log /var/www/cgi-bin/echo.cgi: line 6: /dev/stdin: No such device or address

P.S.2
它为我的作品

P.S.2 it works for me

if [ $REQUEST_METHOD == 'POST' ]; then
    if [ "$CONTENT_LENGTH" -gt 0 ]; then
        while read i; 
            do echo "$i";
        done
    fi
fi

但我有这样的输出

but i have such output

------WebKitFormBoundary8WbSWxBe8rIaCMLG
Content-Disposition: form-data; name="file"; filename="ca.cpp"
Content-Type: text/x-c++src

#include <iostream>
int main(){
for(int x = 0; x <= 5; ++x)
for(int y = 0; y <=5; ++y)
std::cout << "x= " << x << " y= " << y << " f1= " << (x-2)*(x-2) + (y-1)*(y-1) << " f2= " << (x-2)*(x-2) + (y-5)*(y-5)<<std::endl;

}

------WebKitFormBoundary8WbSWxBe8rIaCMLG
Content-Disposition: form-data; name="submit"

Submit
------WebKitFormBoundary8WbSWxBe8rIaCMLG--

我怎么能解析我的文件内容?

How i can parse content of my file?

推荐答案

您要引用变量:

echo "$POST" > /tmp/a

另外,分词和通配符扩展的情况,这将造成的空白将被合并,一切都会在一行。

Otherwise, word splitting and wildcard expansion happens, which will cause whitespace to be merged and everything will be on a single line.

在一般情况下,你应该总是引用变量,除非你有一个很好的理由不这样做。有一些circimstances它不是必要的,但它绝不会伤害除非你真正需要分词。

In general, you should always quote variables unless you have a good reason not to. There are some circimstances where it's not needed, but it never hurts unless you actually need word splitting.

这篇关于巴什CGI上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆