1

I am uploading a zip folder using Laravel after uploading I am extracting this zip folder using the zip archive everything works perfectly but I have a problem with file names in this folder, The extracted folder may contain many files these file names have special characters like " æ Æ Ø " which I want to remove from the file names.

I have used preg_replace but failed to rename the file.

preg_replace('/[ÆØæøÅå]/', '',path/K01_H3_N01 - Længdesnit A-A.pdf);

I only want to remove " æ Æ Ø " these characters from the file name. Thanks in advance.

Salman Iqbal
  • 442
  • 5
  • 23

2 Answers2

1

You can use Storage to save the files to the disk you prefer (S3, local, ...) with Storage::put('file.jpg', $uploaded_file); you can simply store it with a new filename.

UPDATE: As seen in this SO Answer, you can do the following:

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

and then use it like this:

echo clean('a|"bc!@£de^&$f g');
linx
  • 156
  • 2
  • 15
0

You can use the glob function to get list path for all path which where extracted and loop that that list and rename each file by replacing all special char

$filesPaths = glob(dirname(__FILE__) . '/[ÆØæøÅå]/'); 
// This will return an array with old all list of files which pathname matches the given patern

after that you can loop and rename files

foreach($filesPaths as $path){

    // Get the last part of the path which is the name of the file
    $old_name = basename($path); 

     // replace old special char by empty string
    $new_path= preg_replace('/[ÆØæøÅå]/', '', $path);

    // Generate a path by replacing the old file name in the path
    rename($path, str_replace($old_name, basename($newPath), $new_path);
}
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31