Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Created April 19, 2017 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/114aafea3f18cb5cfbb7663bf3aa630c to your computer and use it in GitHub Desktop.
Save Jany-M/114aafea3f18cb5cfbb7663bf3aa630c to your computer and use it in GitHub Desktop.
[WP] Rename files in custom upload folder
<?php
/* ----------------------------------------------------------------------------------------------------------------------------
* PREFACE for this script
*
* A frontend custom form, allowing registered users to create a custom post type + upload images to ACF Gallery custom field,
* in a custom uploads folder, had a bug where it would replace any symbol with -,
* including replacing the file extensions (eg. .jpg and .png, in -jpg and -png).
* That resulted in various issues, even though browsers could still read the file as image.
* After fixing the form itself, this script was made, to fix all the uploads that had already been uploaded and their path.
---------------------------------------------------------------------------------------------------------------------------- */
function replace_filenames() {
$wp_upload = wp_upload_dir();
$wp_upload_dir = trailingslashit($wp_upload["basedir"]);
$wp_upload_url = trailingslashit($wp_upload["baseurl"]);
$folder_name = trailingslashit('progetti'); // this is the custom folder name, inside the uploads folder
$uploads_dir = trailingslashit($wp_upload_dir.$folder_name);
$directories = glob($uploads_dir . '*' , GLOB_ONLYDIR);
// Replace Settings
$change_1 = '-jpg';
$replace_1 = '.jpg';
$change_2 = '-png';
$replace_2 = '.png';
$remove_1 = '-jpg-';
$remove_2 = '-png-';
foreach($directories as $dir) {
if ($handle = opendir($dir)) {
while (false !== ($fileName = readdir($handle))) {
// skip weird stuff
if($fileName == '.' || $fileName == '..')
continue;
// Get img GUI & ID
$dirname = str_replace('/absolute_path_to_/'.$folder_name.'/','',$dir);
$guid = $wp_upload_url.$folder_name.$dirname.'/'.$fileName;
$img_id = attachment_url_to_postid($guid);
// Alternative to the attach to post id function - SQL query for ID & skip if array is empty
/*global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND guid='%s';", $guid));
if(empty($attachment))
continue;*/
// skip if no ID
if($img_id == '' || $img_id == null)
continue;
// skip if file already has the extension in place
$ext = substr($fileName, -4);
if($ext == $replace_1 || $ext == $replace_2)
continue;
// cleanup - remove the wrong stuff from automatically created thumbs
if(preg_match('/'.$remove_1.'/',$fileName)) {
$newName = str_replace($remove_1,'-',$fileName);
rename($dir.'/' . $fileName, $dir.'/' . $newName.$replace_1);
} elseif(preg_match('/'.$remove_2.'/',$fileName)) {
$newName = str_replace($remove_2,'-',$fileName);
rename($dir.'/' . $fileName, $dir.'/' . $newName.$replace_2);
}
// first replacement - jpg
elseif (preg_match('/'.$change_1.'/',$fileName)) {
$newName = str_replace($change_1,$replace_1,$fileName);
rename($dir.'/' . $fileName, $dir.'/' . $newName);
// Replace attachments GUI in database
$new_guid = $wp_upload_url.$folder_name.$dirname.'/'.$newName;
update_attached_file( $img_id, $newName );
}
// second replacement - png
elseif(preg_match('/'.$change_2.'/',$fileName)) {
$newName = str_replace($change_2,$replace_2,$fileName);
rename($dir.'/' . $fileName, $dir.'/' . $newName);
// Replace attachments GUI in database
$new_guid = $wp_upload_url.$folder_name.$dirname.'/'.$newName;
update_attached_file( $img_id, $newName );
}
}
closedir($handle);
}
}
}
// Only run this ONCE, then comment this part out
if(current_user_can('manage_options') && !is_admin()) {
replace_filenames();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment