首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP rename()不工作

PHP rename()不工作
EN

Stack Overflow用户
提问于 2015-10-03 20:05:17
回答 1查看 2K关注 0票数 0

我试图重命名一个重复的文件名时,上传一个新的文件与相同的名称。一切都很好,除了我遇到了一个错误:

代码语言:javascript
复制
<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仍然抛出这个错误。我已经在这里查阅了无数的线程,它们似乎都没有帮助,因为我找不到任何路径或字符串错误与我的文件路径。

感谢您能提供的任何帮助!

欢呼科林

代码:

代码语言:javascript
复制
<?
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; 
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-03 20:19:12

1- 重命名函数至少需要两个参数。看来你只提供了one

2-此外,您还可以使用完整的路径,即:

代码语言:javascript
复制
$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,您的问题可能在这里:

代码语言:javascript
复制
$new_img = RandomString() . '.' . $img_array[sizeof($img_array)-1];

4-尝试设置一个临时硬核值为$new_img,看看是否有效,如果工作,您的错误在那里。

代码语言:javascript
复制
$new_img = "tempfile.jpeg";

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32926966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档