dprnr5559 2017-09-02 21:30
浏览 29
已采纳

将else添加到rename()函数

I'm writing a simple rename form that renames the folder name. It works fine, but if the file doesn't exist PHP gives me an error.

<form> 
    <input type="text"  value="folder name you want to rename">
    <input type="submit">
</form>

And this is my PHP code:

<?php
session_start();
$_SESSION['name'] = $_POST['name'];

rename("../folders/".$_POST['name']."","../renamed/new-name");
?>

The code works perfectly, but I want to add an if/else statement because when I run the script for a folder that does not exist, I get this error.

Does not exist

I want to remove the error, and simply output "doesn't exist".

  • 写回答

3条回答 默认 最新

  • dtebrq0245 2017-09-02 21:35
    关注

    You could always just use a simple if statement to check that rename() worked, since it returns a boolean (true, or false) value. Read more on PHP manual.

    So you could write something along these lines:

    if(rename("../folders/".$_POST['name']."","../renamed/new-name"))
    {
        echo "Folder renamed";
        // more code...
    }
    else
    {
        echo "Folder does not exist.";
        // more code...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条