不能从变量中删除$ext.PHP / MySQL


Cannot remove the $ext from a variable. PHP/MySQL

我的索引页有一个输入字段,您可以在其中创建一个文件。在这个输入字段下,我有一个所有已创建文件的表,如下所示:

Title     | Edit | Delete |
denis.txt   Edit   Delete

所以我想让'something'(我不知道怎么做),所以在表中的标题(denis.txt)不显示扩展名(.txt)。

这是我的索引文件:

<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>
                           </div>
                            </form>
<table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Заглавие</th>
                      <th style="align: center; width: 12%"><div align="center">Редактирай</div></th>
                      <th style="align: center; width: 12%"><div align="center">Изтрий</div></th>
                    </tr>
                  </thead>
                  <tbody>
<?php
echo $_POST['edit'];
$full_path = ".";
$dir = @opendir($full_path) or die("Директорията не може да се отвори.");
while($file = readdir($dir))
{
  if($file == "components" || $file == "index-restricted.php" || $file == "index-error.php" || $file == "generalsettings-success.php" || $file == "exampic" || $file == "all_count.txt" || $file == "assets" || $file == "count_vhod.txt" || $file == "count_nachalo.txt" || $file == "index-invalid.php" || $file == "news-delok.php" || $file == "images" || $file == "auth.php" || $file == "php" || $file == "news.php" || $file == "sponsors.php" || $file == "upload" || $file == "js" || $file == "img" || $file == "dashboard.php" || $file == "logout.php" || $file == "ajax" || $file == "prettify" || $file == "log.php" || $file == "css" || $file == "generalsettings.php" || $file == "nachaloadm.php" || $file == "php_errorlog" || $file == "fonts" || $file == "sql.php" || $file == "edit_file.php" || $file == "delete.php" || $file == "create_file.php" || $file == ".." || $file == "index.php" || $file == "." || $file == "edit.php")
  continue;
?> <?php
  echo "<tr><td><a href='$file'>$file</a></td><td><a href='edit.php?name=$file' class='btn mini purple'><div align='center'><i class='icon-edit'></i>&nbsp;&nbsp;Редактирай</div></a></td><td><div align='center'><a href='delete.php?name=$file' class='btn mini black'><div align='center'><i class='icon-trash'></i>&nbsp;&nbsp;Изтрий</div></a></div></td></tr>";
}
closedir($dir);

?>
                  </tbody>
                </table>

这是我的create_file.php:

<?php
$pre_file_name = $_POST['name'];
$ext = ".txt";
$file_name = $pre_file_name.$ext;
fopen($file_name,'w');
?>
<form class="stdform stdform2" action="edit_file.php" method="POST">
    <label>Въведете новината (текст):</label><span class="field"><textarea class="input-xxlarge" name="edit" cols="1600" rows="10"></textarea></span><p>
    <input type="hidden" name="file_name" value="<?php echo $file_name; ?>">
    <p class="stdformbutton">
    <input class="btn btn-primary" type="submit" value="Създай!">
</p>
</form>

我的edit_file.php:

<?php
$edit = $_POST['edit'];
$file_name = $_POST['file_name'];
$file = fopen($file_name, 'w');
fwrite($file,$edit);
fclose($file);
echo "File Saved! <a href='index.php'>Click here to continue</a>"

?>

我的一个朋友写信给我:

"If you use the explode function to separate the file name from the extension. So:
$array = explode(".",$file);
Then you can echo out:
echo $array[0]; which will give you the file name without the extension.
You may get some problems with this though if you have a dot in the file name, so more than one dot. But who uses dots in the file name anyway, but its something to think about.
Also this method might not be best with the while loop looping round the files in the readdir function.
You may be better using the scandir function which puts the files into any array then you can use a for each loop to loop through the array."

但是我不知道如何使它工作。附注:我想它也可以用$_SESSION来做,但我也不知道怎么做:(

问候,丹尼斯Saidov

我认为这是输出表的代码:

  echo "<tr><td><a href='$file'>$file</a></td><td><a href='edit.php?name=$file' class='btn mini purple'><div align='center'><i class='icon-edit'></i>&nbsp;&nbsp;Редактирай</div></a></td><td><div align='center'><a href='delete.php?name=$file' class='btn mini black'><div align='center'><i class='icon-trash'></i>&nbsp;&nbsp;Изтрий</div></a></div></td></tr>";

要删除扩展名,可以使用pathinfo():

echo pathinfo("aaa.txt", PATHINFO_FILENAME);

所以你的代码变成了:

echo "<tr><td><a href='$file'>". pathinfo($file, PATHINFO_FILENAME) ."</a></td><td><a href='edit.php?name=$file' class='btn mini purple'><div align='center'><i class='icon-edit'></i>&nbsp;&nbsp;Редактирай</div></a></td><td><div align='center'><a href='delete.php?name=$file' class='btn mini black'><div align='center'><i class='icon-trash'></i>&nbsp;&nbsp;Изтрий</div></a></div></td></tr>";