代码未在服务器目录php中创建文件 - php

我正在尝试使用以下代码将新文件写入服务器

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回显将文件显示为资源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的新手,起初我只是在工作

参考方案

我看到$filename = $_POST['fileName'];,这表明我正在处理文件。
旁注:fileNamefilename是不同的,如果输入是这种情况。

这也告诉我您正在使用表单,并且要上传文件。

由于您没有提供其余代码,因此表单需要具有POST方法,包括有效的enctype。

method="post" enctype="multipart/form-data"。确保包括在内。

$filename = $_POST['fileName'];将需要更改为
$filename = $_FILES['fileName'];同时确保其输入还带有fileName name属性。

确保您的表单包含POST方法。
如果省略<form action="">,则窗体默认为GET,并且将以静默方式失败。
在执行<form action="" method="post">时将完全指定它。

$file = fopen('"$filename".php', 'w');
报价错误,应该是
$file = fopen($filename.".php", 'w');

或只是$file = fopen($filename, 'w');-不确定文件是什么。

发布其余代码以确保。
如果我错了,我将其删除。

编辑:测试。

使用以下测试代码为我工作,并创建了一个名为filename1.php的文件,其中包含“ content”。

$_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回显将文件显示为资源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');

CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

通过php表单修改我的xml文件 - php

这是我的xml文件和下面的php代码。我输入了一个输入类型,它将按名字搜索学生。然后将显示有关特定学生的信息,并且将显示另一个按钮更新。问题是我想在那之后修改信息。如何通过标签名称获取元素,以便可以修改有关特定学生的信息?<students> <student> <firstname>John</firstname&…

在ajax之后将页面内容复制到同一页面 - php

我有简单的注册公式,我希望首先使用ajax发送,而不刷新页面以控制是否插入正确的数据,然后仅重定向到其他页面。问题是,当我通过ajax将其发送到同一页面后,一切正常,但是页面内容重复,我可以看到两次...这是我的ajaxfunction registruj () { var name = $('#meno').val(); var pri…

我的PHP脚本中用于删除用户的未定义索引错误 - php

即时通讯试图删除用户页面,但我不知道为什么我得到此错误。Undefined index: bil in C:\wamp\www\FORM\deleteadmin.php on line 4 Undefined index: adminID in C:\wamp\www\FORM\deleteadmin.php on line 5 第4行和第5行看起来像这样,…