这是我的news.php文件:
<form action="create_file.php" method="post" class="form-horizontal"/>
<label class="control-label">Заглавие:</label>
<div class="controls">
<input name="name" type="text" width="40" class="span6 m-wrap" />
<input type="submit" value="Продължи..." class="btn red"></input>
</div>
</form>
这是我的create_file.php
<?php
$pre_file_name = $_POST['name'];
$ext = ".txt";
$file_name = $pre_file_name.$ext;
fopen($file_name,'w');
?>
<form action="edit_file.php" method="post" class="form-horizontal"/>
<label class="control-label">Въведете новината (текст):</label>
<div class="controls">
<span class="field"><textarea class="span6 m-wrap" name="edit" cols="1600" rows="10"></textarea></span><p>
<input type="hidden" name="file_name" value="<?php echo $file_name; ?>">
<input class="btn red" type="submit" value="Създай!">
</div>
</form>
这是我的edit_file.php:
<?php
$edit = $_POST['edit'];
$file_name = $_POST['file_name'];
$file = fopen($file_name, 'w');
fwrite($file,$edit);
fclose($file);
header("Location: news-success.php");
?>
delete.php
<?php
$file = $_GET['name'];
unlink($file);
header('Location: news-delok.php');
?>
edit.php
<?php
$file_name = $_GET['name'];
$file_read = fopen($file_name,"r");
$contents = fread($file_read, filesize($file_name));
fclose($file_read);
?>
<html>
<form action="edit_file.php" method="POST">
<label>Title:</label>
<input class="span6 m-wrap" type="text" placeholder="<?php $array = explode(".",$file_name); echo $array[0] ?>" disabled=""><br>
<label>Text:</label>
<textarea class="span6 m-wrap" name="edit" cols="420" rows="10"><?php echo $contents ?></textarea><p>
<input type="hidden" name="file_name" value="<?php echo $file_name ?>">
<input class="btn red" type="submit" value="Update!">
</form>
如何让脚本检查文件名中是否有空格(例如,新闻1.txt)并用+替换它们(例如,新闻+ 1.txt)?提前谢谢。
答案 0 :(得分:2)
对于 create_file.php ,请执行以下操作(已测试)。
例如:如果您的网页标题为“第1页”且中间有空格,则会创建页面“ page_1.txt ”,并在其间添加下划线。如果需要,可以用下划线代替“ + ”或任何其他有效字符。
<?php
$ext = ".txt";
$pre_file_name = $_POST['name'];
$file_name = $pre_file_name.$ext;
$file_name = str_replace(" ", "_", $pre_file_name.$ext);
fopen($file_name,'w');
?>
<form action="edit_file.php" method="post" class="form-horizontal"/>
<label class="control-label">Enter the new (text):</label>
<div class="controls">
<span class="field"><textarea class="span6 m-wrap" name="edit" cols="1600" rows="10"></textarea></span><p>
<input type="hidden" name="file_name" value="<?php echo $file_name; ?>">
<input class="btn red" type="submit" value="Create one!">
</div>
</div>
</form>
答案 1 :(得分:0)
删除文件名之间的空格使用 str_replace()
在跟随线之后
$pre_file_name = $_POST['name'];
您的代码将
$pre_file_name = $_POST['name'];
$new_file_name = str_replace(" ", "+", $pre_file_name);