为什么文件不能在Apache的CGI中创建
Why file is not creating in CGI with Apache?
我想用Apache的CGI写一些内容到一个文件。
我的HTML文件:
使用Ajax HTTP方法调用CGI脚本。我没有给CGI传递任何参数。最后,我打印响应文本到一个div。
<!DOCTYPE html>
<html>
<head>
<script>
var xhttp;
function loadDoc() {
try{
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("message").innerHTML = this.responseText;
}
};
xhttp.open("POST","mycgi.cgi",true);
xhttp.send();
}catch (exception){
alert("Request failed: " + exception.message);
}
}
</script>
</head>
<body>
<h2>sample ajax</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
<div id ="message" />
</body>
</html>
和cgi.cpp文件
这里我创建了一个写模式的文件,将一些内容写入该文件并成功关闭。发送响应文本"成功写入"返回到HTML。
#include <unistd.h>
#include <iostream>
#include <vector>
#include <string>
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"
#include <stdio.h>
#include <string.h>
#include <fstream>
using namespace std;
using namespace cgicc;
int main(int argc, char **argv)
{
Cgicc cgi;
try {
ofstream myfile;
myfile.open ("Newcgi.txt",ios::out);
cout<<"Content-Type: text/htmlnn";
myfile << "Writing this to a file.n";
cout<<"Sucessfully write";
myfile.close();
}
catch(exception& e) {
}
return 0;
}
但是我看不到在/var/apache2
或/var/www/html
中创建的任何文件。为什么会发生这种情况?应该在哪里创建新文件?是在Apache目录下吗?
代码有问题吗?我也正在写一个文件与写模式。我认为代码工作得很好。有什么建议吗?
前文总结:
- 检查你的CGI文件的权限。通常有一个特殊的用户nobody拥有
- 检查c++ CGI进程是否被Apache击中,它是否通过HTTP返回响应。
- 检查
ofstream::is_open()
返回的内容,它试图创建文件的位置。/tmp
和cgi-bin
工作目录(在httpd.conf
中配置)是存储CGI进程创建的文件的好候选目录,通常是/var/www/cgi-bin/
/var/www
, Apache使用它来执行脚本/进程。相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 为什么文件不能在Apache的CGI中创建
- 用c++ CGI创建环境变量
- 用c语言创建登录功能cgi-html.(没有PHP, Apache)