我有这个代码应该重命名图像。它采用网站的标题并生成一个名称,但如果没有标题,它只保留旧名称。
我怎样才能这样做,而不是标题,它只是randoms数字输入?所以不是" title.extension"它将是randomnumber.jpg
?
$tmp = download_url( $img );
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $img, $matches);
$newfn = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));
$oofnm = basename($matches[0]);
if($newfn != $oofnm) {
$newfn2 = str_replace(array(".jpg", ".png", ".gif"), "", $newfn);
$tmppath = pathinfo( $tmp ); // extract path parts
$newpth = $tmppath['dirname'] . "/". $newfn2 . "." . $tmppath['extension'];
rename($tmp, $newpth); // renames temp file on server
$tmp = $newpth;
}
$file_array['name'] = $newfn;
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
continue;
}
答案 0 :(得分:4)