无法重命名$ _POST中的文件夹

时间:2015-03-22 06:18:43

标签: php post

我在这个网站上浏览了很多参考资料,发现这个功能成功:

<?php 
//tes to rename a sub folder
$oldDir="test";
$newDir = "testing";

if (!is_dir(dirname($newDir))) {
    mkdir(dirname($newDir), 0777, true);
}

rename($oldDir,$newDir);
echo 'successfully renamed into';
?>

现在,我需要重命名类别标签(目录)。但是,它给了我错误: "warning"."No such file or directory in ...."当我在$ _POST中有一个类别名称时,如下图所示:

首先 submit.php

enter image description here

<?php
$displCat = $mydb->prepare("SELECT * FROM labelz ORDER BY label_name");
$displCat->execute();
$dataCat = $displCat->fetchAll();
    foreach ($dataCat as $tampil_cat) {
    $idcat=$tampil_cat['id'];
    $cat=$tampil_cat['label_name'];
    ?>

<form action="u.php?id=<?php echo $idcat; ?>" method="post" name="frmupdatekat">
<div class="modal-body" style="overflow:auto;">
<h4>Edit Kategori: <?php echo $idcat.". ".$cat; ?></h4>
<input type="text" class="form-control" id="txtOldKat" name="txtOldKat" value="<?php echo $cat; ?>" style="border:0px;"/>
<input type="text" class="form-control" id="txtUpdateKategori" name="txtUpdateKategori"/>
<br />
<input type="submit" name="cmdUpdateKategori" id="cmdUpdateKategori" class="btn btn-success" style="vertical-align:middle; height:27px; padding:4px; font:12px Arial;" value="Update Kategori"/>
<br />
<br />
</div>
</form>
//.......
?>

第二次 u.php

<?php
session_start();
include('conn.php');

if (isset($_GET['id'])){
    $id=isset($_GET['id']) ? $_GET['id'] : '';
    $txtOldKat=trim($_POST['txtOldKat']);
    $txtUpdateKategori=trim($_POST['txtUpdateKategori']);

rename($txtOldKat,$txtUpdateKategori);
echo "<script>alert('Haadeeeehhh, ini koq error muluuuuuuuuuuuu!');</script>";
exit(); // stop to see if errors here.

if (isset($_POST['cmdUpdateKategori'])){
    if (empty($_POST['txtUpdateKategori'])){
        echo "<script>alert('Admin, Anda belum mengganti nama Kategori!');</script>";
        echo "<meta http-equiv='refresh' content='0; url=adminz/kategori.php?target=kategori'>";
        exit();
        }
    else{
        $sql="SELECT * FROM labelz WHERE id=:id";
        $sth = $mydb->prepare($sql);
        $sth->execute(array(':id' => $id ));

//....... others code
?>

然后我使用了以前的脚本但是甚至无法使用:

__DIR__
__dirname(__FILE__)
realpath()

我还检查$old_cat是否存在且确实存在。 这是来自filezilla的屏幕截图(也是检查cpanel) enter image description here
然后尝试在帖子值中使用trim()。 我也在这里读到:http://php.net/manual/en/function.rename.php看看我是不是错了。

看来,它在 $_POST内无效 所以,我应该做什么,因为我需要在$_POST内做。 否则,您可能有其他解决方案。

如果您发现这是重复的问题,请告诉我链接。
非常感谢您的建议/建议/解决方案。

1 个答案:

答案 0 :(得分:2)

在代码中执行一些错误检查,确保在重命名之前存在$old_cat,确保$new_cat实际上是有效的文件/文件夹名称且不存在,请确保实际设置了$old_cat$new_cat