0

I have website where I create a file from a form. The user have to write a comapany name and it will create a folder with the company name and save the company name inside my Database, later when some steps are made on my website here is an exemple how I create a folder :

$folder_name = format_folder_name($entreprise->getNom());
$path_file = $path_upload . '/' . $folder_name;
createFolderIfNotExist($path_file);

Recently I had a case where the company is "LALAL & OMOMO" (fake name of course ^^) One folder I've been created correctly but for some reasons it can't work correctly because of the "&" inside the company name. When I want to reuse the folder inside my code it can't find id beacause of what is after the "&" and it is consider as a diffrent command.

how could I save and create folders correctly with the & inside my folder name ? (I don't want to change company name).

here is an exemple from when i want to upload the company logo :

// create folder company name if not exist
$entreprise_folder_name = format_folder_name($_POST["create_entreprise_nom"]);
$path_file = $path_upload . '/' . $entreprise_folder_name;
createFolderIfNotExist($path_file);
// create folder "Logo" if not exist
$path_file .= "/Logo";
createFolderIfNotExist($path_file);

And when I take a look to the folder I have to folder with my company name

  • LALAL_&_OMOMO
  • LALAL__OMOMO

The logo is saved in the second folder

I'm affraid to have the same case with some others caracters like " ' " for example.

NoobieNoob
  • 129
  • 1
  • 2
  • 13
  • Possible duplicate of https://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe – 04FS Jun 14 '19 at 12:23
  • WHat OS are you using I can create directories with that name on Windows and Ubuntu? ... thats all I have access to now – RiggsFolly Jun 14 '19 at 12:26
  • The os is Debian, I canc reate folder with "&" correct but later with my code when it needs to go inside this folder I can't beaucause what is wrote after the "&" is consider as a different command – NoobieNoob Jun 14 '19 at 12:28
  • Wrap dir name in quotes – RiggsFolly Jun 14 '19 at 12:29
  • _“but later with my code when it needs to go inside this folder I can't because what is wrote after the "&" is consider as a different command”_ - please show what you are actually doing at that point. – 04FS Jun 14 '19 at 12:35
  • I've edited my question with an exemple – NoobieNoob Jun 14 '19 at 13:00

1 Answers1

1

Use a regex to replace the invalid folder name with normal characters.

$path_file = $path_upload . '/' . preg_replace("/[^a-z0-9\_\-\.]/i", '', $folder_name);

Regex

Roy Bogado
  • 4,299
  • 1
  • 15
  • 31