使用php表单创建一个名称文件

时间:2013-12-06 13:14:13

标签: php

填写一个表格,它给出了两个部分,文件名和文本我需要在html目录中创建一个名为$ filename的文件,并获取我放入$ text的文本。 不幸的是,我无法让它发挥作用 任何帮助请:

表格代码:

<form method="post" action="createfile.php">
<font size"3"><b>File name:</b> </font>
<br /><input type="text" size="15" name="filename" id="filename"><br />
<font size"3"><b>File data:</b> </font>
<div id="dat"><textarea name="thedata" id="thedata" cols="30" rows="15"></textarea></div>
<div id="sub" style="position:absolute; left: 25">
<input type="submit" value=" Create ">
</div>
</form>

createfile.php代码:

<?php 
$name= $filename; 
$data= $thedata; 
if ( $file && $data ) { 
$fp = fopen($file, "w");  
fwrite($fp, "<br>"); 
fwrite($fp, $data); 
fclose($fp); 
} 
else 
{ echo 'no text entered'; 
} 
?>

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

HTML将是相同的:

<form method="post" action="createfile.php">
<font size"3"><b>File name:</b> </font>
<br /><input type="text" size="15" name="filename" id="filename"><br />
<font size"3"><b>File data:</b> </font>
<div id="dat"><textarea name="thedata" id="thedata" cols="30" rows="15"></textarea></div>
<div id="sub" style="position:absolute; left: 25">
<input type="submit" value=" Create ">
</div>
</form>

PHP:

<?php
$filename = $_POST['filename'];
$content  = $_POST['thedata'] ;
$if(!isset($_POST['filename'])){
    //i leave the filename && content validation to the client side

    $handle = fopen("/files/$filename.txt", "w");
    fwrite($handle, $content);
    fclose($handle);
}else{
    //your `else` code here.
}
?>

答案 1 :(得分:0)

使用以下代码:

<?php
$name= isset($_POST['filename'])?$_POST['filename']:0; 
$data= isset($_POST['thedata'])?$_POST['thedata']:0;  
if ($name && $data ) { 
$fp = fopen($name, "w");  
fwrite($fp, "<br>"); 
fwrite($fp, $data); 
fclose($fp); 
} 
else { 
echo 'no text entered'; 
} 
?>