php - Exchanging name fo random number -
i have code below part of script saves images onto wordpress installation. problem script saves image original name, example "originalimage.jpg". want saved image get's random number assigned like, "randomnumber.jpg" , wonder how can can take taken random number again if used 1 once. tried far breaks code. can please help?
function wpr_save_all_images($content,$keyword,$insert) { $path = wp_upload_dir(); $path = $path['baseurl']; $html = $content; if ( stripos( $html, '<img' ) !== false ) { $regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im'; preg_match_all( $regex, $html, $matches ); if ( is_array( $matches ) && ! empty( $matches ) ) { $new = array(); $old = array(); require_once(abspath . 'wp-admin/includes/file.php'); require_once(abspath . 'wp-admin/includes/media.php'); require_once(abspath . 'wp-admin/includes/image.php'); foreach( $matches[2] $img ) { if ( stripos( $img, "images/buynow-big.gif" ) !== false ) { continue; } $size = getimagesize($img); $width = $size[0]; $height = $size[1]; if ( $width < 2 || $height < 2 ) { continue; } if ( stripos( $img, $path ) !== false ) { continue; } $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; } $id = media_handle_sideload( $file_array, $insert ); if ( ! is_wp_error( $id ) ) { $url = wp_get_attachment_url( $id ); $thumb = wp_get_attachment_thumb_url( $id ); array_push( $new, $url ); array_push( $old, $img ); } //echo "old: ". $img . "<br>wawa: " . $wawawa . "<br>new: " . $url . " <br>"; } if( !empty( $new ) ) { $content = str_ireplace( $old, $new, $html ); $post_args = array( 'id' => $insert, 'post_content' => $content, ); if (!empty($content)) $post_id = wp_update_post( $post_args ); } } } return true; }