我试图重命名一个重复的文件名时,上传一个新的文件与相同的名称。一切都很好,除了我遇到了一个错误:
<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;
}
?>发布于 2015-10-03 20:19:12
1- 重命名函数至少需要两个参数。看来你只提供了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 error_reporting(E_ALL); ini_set('display_errors', 1);标记之后立即将错误报告添加到文件的顶部,然后再添加代码的其余部分,以查看它是否产生任何结果。在生产中移除。
https://stackoverflow.com/questions/32926966
复制相似问题