Rename file while uploading?

I have the following code in my WordPress. I need to add every uploaded image a counting number like, image_1, image_2, image_3 and so on..

The purpose of this is that every uploaded image attached to post, gets post ID name, and counting number in end to it.

Read More

It would be great if some one help me with this. Thanks!

<?php

add_filter('wp_handle_upload_prefilter', 'wpse_25894_handle_upload_prefilter');
add_filter('wp_handle_upload', 'wpse_25894_handle_upload');

function wpse_25894_handle_upload_prefilter( $file )
{
add_filter('upload_dir', 'wpse_25894_custom_upload_dir');
return $file;
}

function wpse_25894_handle_upload( $fileinfo )
{
remove_filter('upload_dir', 'wpse_25894_custom_upload_dir');
return $fileinfo;
}

function wpse_25894_custom_upload_dir($path)
{   
/*
 * Determines if uploading from inside a post/page/cpt - if not, default Upload folder is used
 */
$use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ?   true : false; 
if( !empty( $path['error'] ) || $use_default_dir )
    return $path; //error or uploading not from a post/page/cpt 

/*
 * Save uploads in ID based folders 
 *
 */

 $customdir = '/' . $_REQUEST['post_id'];


$path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
$path['url']     = str_replace($path['subdir'], '', $path['url']);      
$path['subdir']  = $customdir;
$path['path']   .= $customdir; 
$path['url']    .= $customdir;  

return $path;
}
// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'wpse_123240_rename_intermediates' );

function wpse_123240_rename_intermediates( $image ) {

// Split the $image path into directory/extension/name
$info = pathinfo($image);
$dir = $info['dirname'] . '/';
$ext = '.' . $info['extension'];
$name = wp_basename( $image, "$ext" );

// Get image information 
// Image edtor is used for this
$img = wp_get_image_editor( $image );

// Build our new image name
$postid = $_REQUEST['post_id'];
$random = rand(1,5);

$new_name = $dir . $postid . '_' . $random . $ext;

// Rename the intermediate size
$did_it = rename( $image, $new_name );

// Renaming successful, return new name
if( $did_it )
return $new_name;
return $image;
}
?>

Now this code generates images named postid_randomnumber.jpg

I just need to add 20 images at maximum, so if I can have numbers from 1-20, that is also working fine with my purposes.

— UPDATE —

I canged the last part of code to this, it is not maybe the cleanest solution, but it works:

function wpse_123240_rename_intermediates( $image ) 

{
// Split the $image path into directory/extension/name
$info = pathinfo($image);
$dir = $info['dirname'] . '/';
$ext = '.' . $info['extension'];
$name = wp_basename( $image, "$ext" );

// Get image information 
// Image edtor is used for this
$img = wp_get_image_editor( $image );

//$count = get_option( 'wpa59168_counter', 1 );

// Build our new image name
$postid = $_REQUEST['post_id'];
$increment = 1;

$new_name = $dir . $postid . '_1' . $ext;

while(is_file($new_name)) {
 $increment++;
 $new_name = $dir . $postid . '_' . $increment . $ext;
}

// Rename the intermediate size
$did_it = rename( $image, $new_name );

// Renaming successful, return new name
if( $did_it )
    return $new_name;

return $image;

}

Related posts

Leave a Reply