How to Change Filename How to Change Filename
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

How to Change Filename

Started by formadmirer, August 11, 2007, 03:57:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

formadmirer

I would like to change the filename of uploaded pics. I don't want to change only the data displayed, but the actual filename that gets stored in the db. Ideally, the new name would be in the following format:

owner id."-".picture id."-".ctime.extension

i.e., 12-44-185799794.jpg

Also, if keywords were submitted empty I would like to fill the keywords with the title.

I can write something that will go into the db after the files have been uploaded and do this, but I really need to have it done at the time of upload.

cpg 1.4.12


formadmirer

Don't want to reply to my own post but I didn't see an edit button.

Anyway, got the second part done (using title as keywords if keywords submitted blank). Extremely easy. So far I am just thrilled with Coppermine.

I am still working on renaming the filename, but so far have nothing. I could definately use a hand on that.

Joachim Müller

Coppermine doesn't change the filename once a file has been uploaded, as it can be tricky, so there is nothing built into coppermine that will do that. I don't recommend changing the filenames around in the first place. If you (as admin) are the only one who uploads to your gallery I suggest naming your files accordingly before upload. If you expect users to upload, you'll have to figure out what upload method they use and add a function right after they have uploaded their files. As suggested, this is not recommended: imo the human doesn't need to keep track of file names - that's what coppermine does for the human just fine.
As far as I know there is a plugin or mod that will rename files, so I suggest searching for it.
In the future, please respect the "one question per thread" policy that you agreed to respect when signing up.

formadmirer

Unfortunately the gallery is open to public upload. What I am trying to avoid is the problem I have had previously with people uploading files with names that are extremely long, duplicated over and over, or filled with bad characters. I figured if they could upload and then I take care of naming the file that these potential problems would be eliminated.
But, since after searching the forums I found only one other request for the same thing, I am beginning to think this may not be a (potential) problem that warrants any of my current time to fix.

I was unable to find any plugin or mod to accomplish this task, so I am going to move on and not worry about this hack for now.

As for the one question per thread, I aplogize. I post on many forums and have never encountered a one question rule, but I will respect it in future posts.
After reading through dozens of posts here I knew you were going to call me on this.  ;)
Actually, I also fully expected you to move this thread to themes.

Thanks for your help.


Joachim Müller

Quote from: formadmirer on August 12, 2007, 03:33:54 AM
What I am trying to avoid is the problem I have had previously with people uploading files with names that are extremely long, duplicated over and over, or filled with bad characters.
Use the "Characters forbidden in filenames" option on coppermine's config page to keep people from using funny characters. If you want to truncate file names, take a look at include/functions.inc.php and edit the function definitionfunction replace_forbidden($str)
{
  static $forbidden_chars;
  if (!is_array($forbidden_chars)) {
    global $CONFIG, $mb_utf8_regex;
    if (function_exists('html_entity_decode')) {
      $chars = html_entity_decode($CONFIG['forbiden_fname_char'], ENT_QUOTES, 'UTF-8');
    } else {
      $chars = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;', '&nbsp;', '&#39;'), array('&', '"', '<', '>', ' ', "'"), $CONFIG['forbiden_fname_char']);
    }
    preg_match_all("#$mb_utf8_regex".'|[\x00-\x7F]#', $chars, $forbidden_chars);
  }
  /**
   * $str may also come from $_POST, in this case, all &, ", etc will get replaced with entities.
   * Replace them back to normal chars so that the str_replace below can work.
   */
  $str = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $str);;
  $return = str_replace($forbidden_chars[0], '_', $str);

  /**
  * Fix the obscure, misdocumented "feature" in Apache that causes the server
  * to process the last "valid" extension in the filename (rar exploit): replace all
  * dots in the filename except the last one with an underscore.
  */
  // This could be concatenated into a more efficient string later, keeping it in three
  // lines for better readability for now.
  $extension = ltrim(substr($return,strrpos($return,'.')),'.');
  $filenameWithoutExtension = str_replace('.' . $extension, '', $return);
  $return = str_replace('.', '_', $filenameWithoutExtension) . '.' . $extension;

  return $return;
}
as you see fit.