Skip to content

Commit

Permalink
fix: trim nasty characters from filenames
Browse files Browse the repository at this point in the history
Fix up makeEpisodeFilename() and makeUniqueFilename() so that characters
outside the alphanumeric range that aren't the period, dash, or
underscore are replaced with an underscore.

refs #691
  • Loading branch information
Chris Charabaruk committed Sep 12, 2022
1 parent 2c2e049 commit fc058e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PodcastGenerator/core/episodes.php
Expand Up @@ -409,7 +409,7 @@ function compare_mtimes($a, $b)
*/
function makeEpisodeFilename($directory, $date, $filename)
{
$filename = strtolower(str_replace(' ', '_', $filename));
$filename = strtolower(trim(preg_replace('/[^a-zA-Z0-9._-]+/', '_', $title), '_'));
$targetfile = $directory . $date . '_' . $filename;

if (file_exists($targetfile)) {
Expand Down
3 changes: 2 additions & 1 deletion PodcastGenerator/core/misc/functions.php
Expand Up @@ -99,7 +99,8 @@ function makeUniqueFilename($path)
// make sure that we have a real directory path with filename attached
// just putting $path into realpath() will fail if the file doesn't exist
$pathinfo = pathinfo($path);
$realpath = realpath($pathinfo['dirname']) . '/' . $pathinfo['basename'];
$filename = strtolower(trim(preg_replace('/[^a-zA-Z0-9_-]+/', '_', $pathinfo['filename']), '_'));
$realpath = realpath($pathinfo['dirname']) . '/' . $filename . $pathinfo['extension'];

// if the existing path doesn't exist, we're unique!
if (!file_exists($realpath)) {
Expand Down

0 comments on commit fc058e0

Please sign in to comment.