代码没有在服务器目录php中创建文件


Code is not creating a file in server directory 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页面,html内容正在使用jquery post复制,在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名称属性。

  • 确保你的表单包含POST方法。
  • 表单默认为GET,如果省略<form action="">,将静默失败。
  • 当执行<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>


编辑2:

$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');

PHP将只解析双引号内的变量,所以在您的情况下,您需要这样做:

$file = fopen("'"$filename'".php", 'w');

$file = fopen($filename.'.php', 'w');

还需要确保PHP有足够的权限创建文件

quotes中,它将是

 $file = fopen($filename.".php", 'w');

还要阅读单引号和双引号

冰天雪地的

$filename = "text.php";
echo '$filename'; // $filename.
echo "$filename"; // text.php.