我尝试使用以下代码将新文件写入服务器
error_reporting(E_ALL); ini_set('display_errors', 1);
if($_SERVER['REQUEST_METHOD'] == "POST") {
$html = $_POST['html'];
$filename = $_POST['fileName'];
$file = fopen('"$filename".php', 'w');
fwrite($file, $html);
// fwrite($file, $_POST["cssContent"]);
fclose($file);
正在创建的文件是一个新的html页面,并且正在使用jquery post复制html内容,在echo语句中我可以看到html就在那里以及我想要调用这个新文件的文件名,但是之后运行脚本文件尚未创建,并且php错误报告未报告错误。 $ file echo将文件显示为Resource id#3
html网页代码
<?php
include_once '../../../includes/db_connect.php';
include_once '../../../includes/functions.php';
sec_session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$first_name = $_SESSION['memberInfo']['memberFirstName'];
$surname = $_SESSION['memberInfo']['memberLastName'];
$hash = $_SESSION['memberInfo']['hash'];
$newTemplateSrc = $_SESSION['memberInfo']['templateSrc'];
$sql = "SELECT * FROM members WHERE firstName = '$first_name' AND surName = '$surname' AND passWord = '$hash' AND templateFileSrc = '$newTemplateSrc'";
$result=mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result) ){
$dateofBirth = $row['dateofBirth'];
$deceasedName = $row['deceasedName'];
$dateofDeath = $row['dateofDeath'];
$aboutDeceased = $row['aboutDescription'];
$directoryId = $row['directoryid'];
$templateFileSrc = $row['templateFileSrc'];
$deceasedImage = $row['deceasedPhoto'];
};
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- TemplateBeginEditable name="doctitle" -->
<title>RIDERS in the SKY CREATE MEMORIAL</title>
<!-- TemplateEndEditable -->
<link rel="stylesheet" type="text/css" href="../../../style/rits2.css">
<link rel="stylesheet" type="text/css" href="../../../Valums-File-Uploader-file-uploader-9991748/client/fileuploader.css">
<style type="text/css">
a:link {
text-decoration: none;
color: rgba(135,206,235,1);
}
a:visited {
text-decoration: none;
color: rgba(135,206,235,1);
}
a:hover {
text-decoration: none;
color: rgba(0,0,0,1);
}
a:active {
text-decoration: none;
color: rgba(135,206,235,1);
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
<div class="container">
<header>
<table id="head_top">
<tbody>
<tr>
<td width="10%" rowspan="3" class="logo"><a href="../../../index.html" target="_self"><img src="../../../images/riders-in-the-sky.png" alt="RIDERS IN THE SKY LOGO - CLICKING HERE TAKES YOU TO THE HOME PAGE" title="RIDERS IN THE SKY LOGO - CLICKING HERE TAKES YOU TO THE HOME PAGE" class="logo_image"/></a></td>
<td width="80%" colspan="3" class="title">RIDERS IN THE SKY </td>
<td width="10%" rowspan="3" class="login_register"><table class="log_nav">
<tbody>
<tr>
<td><form action="../../../register.php"><input name="register" type="submit" class="register" title="REGISTER HERE" id="register" value="Register"></form></td>
</tr>
<tr>
<td><form action="../../../login.php"><input name="login" type="submit" class="login" title="LOGIN HERE" id="login" value="Login"></form>
</td>
</tr>
<tr> </tr>
</tbody>
</table></td>
</tr>
<tr>
<td colspan="3" class="title_tagline">MEMORIALS AND FUNERAL RESOURCES FOR BIKERS AND MOTORCYCLISTS</td>
</tr>
<tr>
<td><div class="3but_nav volunteer"><a href="../../../volunteer.php" title="VOLUNTERR HERE">VOLUNTEER</a></div></td>
<td><div class="3but_nav resource hover"><a href="../../../resources.php" title="FIND FUNERAL RESOURCES HERE">FUNERAL RESOURCES</a></div></td>
<td><div class="3but_nav about"><a href="../../../about.php" title="FIND OUT MORE ABOUT RIDERSin the SKY HERE">ABOUT US</a></div></td>
</tr>
</tbody>
</table>
<table class="top_nav">
<tbody>
<tr>
<td class="create"><form><input name="create" type="button" class="create_but" id="create" value="Create Memorial" title="CTREATE A MEMORIAL HERE" onClick="window.location.href='../../../create.php'"></form></td>
<td class="view"><form><input name="view" type="button" class="view_but" id="view" value="View Memorials" title="VIEW A MEMORIAL HERE" onClick="window.location.href='../../../view.php'"></form></td>
<td class="faq"><form><input name="faq" type="button" class="faq_but" id="faq" value="Faq's" title="GO TO OUR FAQ's and HELP PAGE" onClick="window.location.href='../../../faq.php'"></form></td>
<td class="contact"><form><input name="contact" type="button" class="contact_but" id="contact" value="Contact Us" title="NEED TO CONTACT US? CONTACT US HERE" onClick="window.location.href='../../../contact.php'"></form></td>
<td class="donate"><form><input name="donate" type="button" class="donate_but" id="donate" value="Make a Donation" title="WANT TO MAKE A DONATION TO RIDERSin the SKY? GO TO OUR DONATIONS PAGE" onClick="window.location.href='../../../donation.php'"></form></td>
</tr>
</tbody>
</table>
</header>
<!-- TemplateBeginEditable name="content_main" -->
<main class="content">
<div id="deceasedName" class="#" contenteditable="false"><?php echo $deceasedName ?></div>
<div id="DeceasedDetail" contenteditable="false">
<div id="dob" contenteditable="false"><?php echo $dateofBirth ?></div>
<div id="deceasedImage" contenteditable="false"><img class="deceasedImage" src="<?php echo $deceasedImage ?>"></div>
<div id="dod" contenteditable="false"><?php echo $dateofDeath ?></div>
<div id="deceasedProfile">
<h1 id="aboutTitle" contenteditable="false" >About <?php echo $deceasedName?></h1>
<h2 id="aboutDesciption" contenteditable="false" ><?php echo $aboutDeceased ?></h2>
</div>
<input name="button" type="button" class="editMemorial" value="Edit/Create Memorial">
</div>
<div class="editMenu" style="display:none">
<button id="editDOB" class="editMenuNav">Edit/Add D.O.B</button>
<button id="editPic" class="editMenuNav">Change/Add Deceased Photo</button>
<button id="editDOD" class="editMenuNav">Edit/Add Deceased D.O.D</button>
<button id="editDeceasedTitle" class="editMenuNav">Edit/Add About the Deceased Title</button>
<button id="editDeceasedDescription" class="editMenuNav">Edit/Add About the Deceased</button>
<button id="editName" class="editMenuNav">Edit/Add Deceased Name</button>
<button id="saveEdits" class="editMenuNav">Save Changes</button>
</div>
</main>
<!-- TemplateEndEditable -->
<footer class="footer">
<div class="breadcrumb">
<p class="breadcrumb"><a href="../../../resources.php">Funeral Resources</a> | <a href="../../../terms.php">Terms of Use</a> | <a href="../../../privacy.php">Privacy</a> | <a href="../../../about.php">About Us</a> | <a href="../../../contact.php">Contact Us</a> | <a href="../../../faq.php">Faq's</a></p>
<p class="copyright"><span>©2015 RIDERS IN THE SKY</span></p>
</div>
</footer>
</div>
<div id="uploadDeceasedImage" style="display: none">
<div id="closeDeceasedUpload">Close Upload X</div>
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
or put a simple form for upload here
</noscript>
<div id="preview">
<img src="../../../images/riders in the sky no text.png" alt="" width="100px" height="100px" class="deceasedThumb"/>
</div>
<!--<form id="deceasedImageUpload">
<label class="deceasupload">Upload a Picture of the Deceased</label>
<input type="file" size="20" id="imageUpload" class=" ">
<button type="submit" class="saveDeceasedImage">Save Photo</button>
</form>-->
<div id="uploadDeceasedImageWrapper">
</div>
<input id="file_upload" name="file_upload" type="file" multiple style="display:none">
<input type="button" id="upload_but" href="javascript:$('#file_upload').uploadifive('upload')" style="display: none" value="Upload Images">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="../../../Valums-File-Uploader-file-uploader-9991748/client/fileuploader.js"></script>
<script type="text/javascript">
$(".editMemorial").on('click', function() {
$('.editMenu').show();
$('.editMemorial').hide();
});
$("#saveEdits").on('click', function() {
var dateofBirth = ($('#dob').text());
var deceasedName = ($('#deceasedName').text());
var dateofDeath = ($('#dod').text());
var aboutTitle = ($('#aboutTitle').text());
var aboutDescription = ($('#aboutDesciption').text());
var firstName = ("<?php echo $first_name?>");
var surName = ("<?php echo $surname?>");
var hash = ("<?php echo $hash?>");
var templateSrc = ("<?php echo $newTemplateSrc?>");
// console.log(dateofBirth, deceasedName, dateofDeath, abouttheDeceased,firstName, surName, hash, templateSrc);
$.post('../../../includes/editContent.php', {dateofBirth:dateofBirth,deceasedName:deceasedName,dateofDeath:dateofDeath, aboutTitle:aboutTitle,aboutDescription:aboutDescription,firstName:firstName,surName:surName,hash:hash,templateSrc:templateSrc }, function(json) {
if(json.result === "success") {
$('.editMenu').hide();
$('.editMemorial').show();
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#dob').attr('class','#')
$('#dod').attr('class','#')
$('#aboutDesciption').attr('class','#')
$('#aboutTitle').attr('class','#')
$('#deceasedName').attr('class','#')
$('#uploadDeceasedImage').hide();
}else{
};
});//json call
});//onclick
// editable clicks
$("#editDOB").on('click', function() {
$('#dob').attr('contenteditable', 'true');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','deceasedNameHighlight');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','#');
$('#uploadDeceasedImage').hide();
});
$("#editName").on('click', function() {
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'true');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','#');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','deceasedNameHighlight');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','#');
$('#uploadDeceasedImage').hide();
});
$("#editDOD").on('click', function() {
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'true');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','#');
$('#dod').attr('class','deceasedNameHighlight');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','#');
$('#uploadDeceasedImage').hide();
});
$("#editDeceasedDescription").on('click', function() {
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'true');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','#');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','deceasedNameHighlight');
$('#aboutTitle').attr('class','#');
$('#uploadDeceasedImage').hide();
});
$("#editDeceasedTitle").on('click', function() {
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'true');
$('#dob').attr('class','#');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','deceasedNameHighlight');
$('#uploadDeceasedImage').hide();
});
$('#editPic').on('click', function() {
$('#uploadDeceasedImage').show();
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','#');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','#');
});
$('#closeDeceasedUpload').on('click', function() {
$('#uploadDeceasedImage').hide();
$('#dob').attr('contenteditable', 'false');
$('#dod').attr('contenteditable', 'false');
$('#deceasedName').attr('contenteditable', 'false');
$('#aboutDesciption').attr('contenteditable', 'false');
$('#aboutTitle').attr('contenteditable', 'false');
$('#dob').attr('class','#');
$('#dod').attr('class','#');
$('#deceasedName').attr('class','#');
$('#aboutDesciption').attr('class','#');
$('#aboutTitle').attr('class','#');
});
</script>
<script type="text/javascript">
// DOM-ready event is a much option here, just using onload in demo for simplicity
// jQuery users can use $(function()) { ...
window.onload = function() {
var uploader = new qq.FileUploader({
// pass the dom node (ex. $(selector)[0] for jQuery users)
element: document.getElementById('uploadDeceasedImageWrapper'),
// path to server-side upload script
// action: '/server/upload'
action: '../../../includes/uploadDeceased_image.php',
params: {
directoryId: '<?php echo $directoryId ?>',
templateSrc: '<?php echo $templateFileSrc ?>'
},
allowedExtensions: ['jpg', 'png'],
sizeLimit: 100000000, // 100mb
minSizeLimit: 500,
debug: true
});
};
</script>
<script type="text/javascript">
$('#saveEdits').on('click', function() {
var html = $("html").html();
var fileName = <?php echo $directoryId ?>;
console.log(html);
$.post('../../../includes/memorialSave.php', {html:html, fileName:fileName}, function(json) {
});
});
</script>
</body>
</html>
html和文件名由jquery代码发送到php,如下所示
<script type="text/javascript">
$('#saveEdits').on('click', function() {
var html = $("html").html();
var fileName = <?php echo $directoryId ?>;
console.log(html);
$.post('../../../includes/memorialSave.php', {html:html, fileName:fileName}, function(json) {
});
});
</script>
我知道有很多jquery可以更好地编写脚本,但我是jquery的新手,最初我只是让工作得到了工作
答案 0 :(得分:1)
TemplateDirectiveModel
中的错误
quotes
另请阅读此{{3}}
适用于考试
$file = fopen($filename.".php", 'w');
答案 1 :(得分:1)
<击>
我看到$filename = $_POST['fileName'];
,这告诉我我们正在处理文件
旁注: fileName
和filename
不一样,如果是输入的话。
这也告诉我你正在使用表单并想要上传文件。
由于您尚未提供其余代码,因此您的表单需要有一个POST方法,包括有效的enctype。
即。 method="post" enctype="multipart/form-data"
。确保包含在内。
此$filename = $_POST['fileName'];
需要更改为
$filename = $_FILES['fileName'];
,同时确保其输入也带有fileName
name属性。
击>
<form action="">
,表单默认为GET,并且将无声地失败。<form action="" method="post">
时会完全指定。另外$file = fopen('"$filename".php', 'w');
报价是错误的,应该是
$file = fopen($filename.".php", 'w');
或只是$file = fopen($filename, 'w');
- 不确定文件是什么。
编辑:测试。
使用以下测试代码,为我工作并创建了一个名为filename1.php
的文件,其中包含“内容”。
$_POST['html'] = "content";
$_POST['fileName'] = "filename1";
$html = $_POST['html'];
$filename = $_POST['fileName'];
$file = fopen($filename.".php", 'w');
一如既往
$_POST['html'] = "<b>content</b>";
里面有<b>content</b>
。
第二次修改:
$ file echo将文件显示为资源ID#3
您尚未向我们展示您尝试从中提取该内容/文件的位置。
答案/示例来自: https://stackoverflow.com/a/2408630/
文件确实打开很好,你不能像那样回应它,因为它是文件指针,而不是文件本身的内容。您需要使用fread()
来阅读实际内容,或者更好的是,使用file_get_contents()
直接获取内容。
按照自己的方式行事:
$handle = fopen("test.txt", "r");
$fileip = fread($handle, filesize($filename));
fclose($handle);
echo $fileip;
或者,使用file_get_contents()
:
$fileip = file_get_contents("test.txt");
echo $fileip;
路径示例:
$path = "/var/user/home/httpdocs/folder_to_write_in/";
$file = fopen($path.$filename.".php", 'w');
答案 2 :(得分:1)
PHP只会在双引号内解析变量,所以在你的情况下你需要这样做:
$file = fopen("\"$filename\".php", 'w');
或
$file = fopen($filename.'.php', 'w');
您还需要确保PHP具有足够的权限来创建文件