【问题标题】:PHP rename function errorPHP重命名函数错误
【发布时间】:2018-08-29 06:34:15
【问题描述】:

我在使用 basename() 获取文件名后尝试重命名文件。我收到一条错误消息,提示“找不到指定的文件”。 我的代码是:

$target_dir = "uploads/";

$base_name = basename($_FILES["file_to_upload"]["name"]);
$target_file = $target_dir.$base_name;//specifies the path of the file to be 
uploaded.
$image_extention = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

//rename the file
$rename_file_name = rename($base_name,'Image_1');
dd($rename_file_name);

当我运行代码时,我收到以下错误:

警告:重命名(RageFace.jpg,Image_1):系统找不到指定的文件。 (代码:2)在 C:\xampp\htdocs\pdo\file_upload_handle.php 第 23 行

布尔(假)

【问题讨论】:

  • 您是否尝试过使用文件的完整路径而不仅仅是basename()
  • 如果您read the manual,它将表明您需要提供文件的完整路径,而不仅仅是文件名,除非文件恰好位于 当前工作目录 中,这对于上传的文件是不明智的
  • “系统找不到指定的文件”对我来说似乎很清楚。检查你的路径。
  • @kerbholz 我之前没有尝试给出完整的路径,我现在已经给出并且它正在工作。
  • @Martin 谢谢。您的评论帮助我理解了问题和我的错误。

标签: php rename


【解决方案1】:

试试

移动时更改名称

move_uploaded_file($_FILES["file_to_upload"]["tmp_name"],$target_file);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2011-02-20
    相关资源
    最近更新 更多