[英]PHP rename() not working
I am trying to rename a duplicate file name upon upload of a new file with the same name. 我试图在上传具有相同名称的新文件时重命名重复的文件名。 Everything is working great except for that I am getting an error:
一切工作正常,除了出现错误:
<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 />
Despite confirming that the directory exists and that the file and directory are both writeable, PHP is still throwing this error. 尽管确认目录存在并且文件和目录都是可写的,PHP仍然抛出此错误。 I have consulted countless threads on here already and none of them seem to help, as I cannot find any path or string error with my file path.
我已经在这里咨询了无数线程,它们似乎都无济于事,因为我的文件路径找不到任何路径或字符串错误。
Thank you for any help you can provide! 感谢您提供任何帮助!
Cheers Colin 干杯科林
Code: 码:
<?
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 - The rename function requires, at least, two arguments. 1- 重命名功能至少需要两个参数。 It seems that you're providing just
one
. 看来您只提供了
one
。
2 - Also, you may want to use the full path, ie: 2-另外,您可能要使用完整路径,即:
$path = '/full/path/to/uploads';
$new_path = '/full/path/to/uploads';
rename("$path/484360_438365932885330_1444746206_n.jpeg", "$new_path/newfile.jpeg");
3 - Make sure $new_img
is set, your problem may be here: 3-确保设置了
$new_img
,您的问题可能在这里:
$new_img = RandomString() . '.' . $img_array[sizeof($img_array)-1];
4 - Try setting a temporary hardcored valued to $new_img
and see if works, if it does, your error is there. 4-尝试将临时性硬核值设置为
$new_img
,看看是否$new_img
,如果可行,您的错误在那里。
$new_img = "tempfile.jpeg";
TIP: Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1);
提示:在打开PHP标记后,将错误报告添加到文件顶部,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
then the rest of your code, to see if it yields anything. 然后其余的代码,看看是否能产生任何结果。 Remove in production.
在生产中移除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.