sky67ro
sky67ro

Reputation: 1

rename files on upload in wordpress 3.4.1

I have been trying to rename my images on upload and only piece of code I found was the one that uses a 32char hash.

function make_filename_hash($filename) {
  $info = pathinfo($filename);
  $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
  $name = basename($filename, $ext);
  return md5($name) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);

My php knowledge is very limited and I looked everywhere for an example that uses some random number being generated instead of a hash, without success. My initial idea was to get the db image id and use it as image filename on upload but I got my brain twisted in the process.

Anyway at this point I would settle for some sort of numbering system that will increase by one digit on every upload or if that is not possbile at least some random number by using a range, like rand(10000,99999).

Any help would be very appreciated thanks a lot

================================================================================= Ok thanks for your help Eric, this is what I got so far and seems to work better. Is there any way I can make it contiguous though? Obviously not using mt_rand :) Something like increase by 1 with every upload. Also is it possible to have the same number generated twice, because that might create issues on upload.

function make_filename_hash($filename) {
  $info = pathinfo($filename);
  $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
  return mt_rand(10,10000) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);

================================================================================= Can you read data from a custom table in functions.php and write to it. I'm trying to read a counter from a table set the image to that counter number and update the counter in the db. It doesn't seem to work, the counter increases but the table never gets updated, and no matter what the value in the db is, the counter does not use that value, so this means it's not reading this value either. I just want to know if there is another way to do this to rename files but with a value from my custom table. This is what I got so far. I was thinking maybe I use the wrong hook but at this point I am a bit stuck. Thanks guys.

 function make_filename_counter($filename) {      
    $qry;
    $fcnt = 0;
    $sql = ("SELECT counter FROM bwbps_filenamecounter WHERE id = 1");
    $cnt = mysql_query($sql);
    foreach($cnt as $cnt) {
        $fcnt = $cnt->counter;
    }
    $fcnt = $fcnt + 1;
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $sql = "UPDATE bwbps_filenamecounter SET counter = " . $fcnt . " WHERE id = 1";
    $qry = mysql_query($sql);
    $fcnt = 0;
    return $fcnt . $ext;
}
add_filter('sanitize_file_name', 'make_filename_counter', 10); 

Upvotes: 0

Views: 1047

Answers (1)

Eric G
Eric G

Reputation: 4058

Based on the answers to this question, I would suggest something like:

function make_filename_hash($filename) {
  $info = pathinfo($filename);
  $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
  $uniqueName = uniqid(basename($filename, $ext));
  return $uniqueName . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);

This will use the original file name as a base and then append a time-sensitive identifier to that. See here for a description of PHP's uniqid() function.

I'm not positive, but I believe WordPress applies the sanitize_file_name filter before anything is saved to the database. If that is the case, you won't be able to tie the new name to the database ID.

EDIT

To return a unique identifier that doesn't incorporate the original file name:

    function make_filename_hash($filename) {
      $info = pathinfo($filename);
      $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
      return uniqid() . $ext;
    }

Upvotes: 1

Related Questions