I'm creating an image upload form and I need to name the images the user's name and dog's name when it is added into my database, and added into my upload folder.
I currently have this working like this:
- I use the POST method at the top of my script to pull in the user name and dog name from the form.
- I then use that info to rename the file/image being uploaded
- Then I insert into my database
$user_name = $_POST['user_name'];
$dog_name = $_POST['dog_name'];
$file_name = $imgDir . $user_name .'-'. $dog_name .'-'. $random_number . $ext;
When inserting into the table I use the following:
'file_name' => str_replace("../path/to/images/", "", $file_name),
The current output of the file name is like this: John-Fido-123456.jpg
How would I remove the spaces in file name if the user entered their last name like this?
John Doe-Fido Doe-123456.jpg
Update: Thanks to @HiDeo i used the dup link that he posted and was able to figure it out like this.
Using the Post method to grab the names I needed. I striped the white spaces like so and it worked on the file name in the database and the actual image in the folder.
$user_name = $_POST['user_name'];
$dog_name = $_POST['dog_name'];
$user_name = preg_replace('/\s+/', '', $user_name);
$dog_name = preg_replace('/\s+/', '', $dog_name );