PHP重命名()无效

时间:2015-10-03 20:05:17

标签: php wordpress rename

我正在尝试在上传具有相同名称的新文件时重命名重复的文件名。一切都很好,除了我收到错误:

<b>Warning</b>:  rename(./uploads/484360_438365932885330_1444746206_n.jpeg,): No such file or directory in <b>/home/unisharesadmin/public_html/wp-content/themes/blankslate/check_duplicate_img.php</b> on line <b>36</b><br />

尽管确认目录存在并且文件和目录都是可写的,但PHP仍然会抛出此错误。我已经在这里咨询了无数线程,但似乎没有任何帮助,因为我找不到文件路径的任何路径或字符串错误。

感谢您提供任何帮助!

干杯 科林

代码:

<?
require_once('../../../wp-config.php');

function RandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

$this_img = $_REQUEST['filename'];

$path = './uploads/' . $this_img;

$img_array = explode(".", $this_img);

$new_img = RandomString() . '.' . $img_array[sizeof($img_array)-1];

$new_path = './uploads/' . $new_img;

if (file_exists($path))
{
    query_posts('posts_per_page=-1');

    while(have_posts())
    {
      the_post();

      if( strpos(get_post_meta(get_the_id(), 'imgurl1')[0], $this_img) !== false )  
      {
        //echo "this posts url1 matches, so update the existing files name and the posts refrence to it";
        echo is_writeable("./uploads");
        rename($path, $newpath);
        //echo update_post_meta(get_the_id(), 'imgurl1', get_template_directory_uri() . '/uploads/' . $new_img);

      }
      else if( strpos(get_post_meta(get_the_id(), 'imgurl2')[0], $this_img) !== false )  //this posts url2 matches
      {
        echo "this posts url2 matches, so update the existing files name and the posts refrence to it";

        //rename($path, $newpath);
        //echo update_post_meta(get_the_id(), 'imgurl2', get_template_directory_uri() . '/uploads/' . $new_img);

      }

    }
}
else
{
 echo 0; 
}

?>

1 个答案:

答案 0 :(得分:2)

1 - rename函数至少需要两个参数。您似乎只提供了one

2 - 此外,您可能希望使用完整路径,即:

$path = '/full/path/to/uploads';
$new_path = '/full/path/to/uploads';
rename("$path/484360_438365932885330_1444746206_n.jpeg", "$new_path/newfile.jpeg");

3 - 确保$new_img已设置,您的问题可能在此处:

$new_img = RandomString() . '.' . $img_array[sizeof($img_array)-1];

4 - 尝试将临时硬编码值设置为$new_img并查看是否有效,如果有效,则表示您的错误存在。

$new_img = "tempfile.jpeg";

提示 在打开PHP标记之后立即将错误报告添加到文件的顶部,例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是代码的其余部分,以查看它是否产生任何结果。在生产中删除。