0

I recently create a custom component with Component Creator, and I have a form in admin area that uploads file...

But I can't upload files with same name it's going to replace the previous file, now I want to rename the file on upload not moving them is the best idea?

I don't know how to rename a file on upload, I've used the save() function in my component controller but it didn't work.

  • 1
    Why not simply check to see if a file of that name already exists and if so, add a _1 on to the end. Of course, you will have to increment the number in a while loop – Lodder Mar 14 '16 at 12:29
  • could you please share the code and say where should i put it .... in component controller ??? if that's works it's a big help, thanks – Arash Yazdani Mar 14 '16 at 12:36
  • check this here - stackoverflow.com/questions/12909324/… – Joomler Mar 14 '16 at 13:19
  • @Joomla, I'd recommend linking to this answer: stackoverflow.com/a/19431372/1362108 ....The answers that have been upvoted are using old, deprecated methods – Lodder Mar 14 '16 at 13:23
  • 4
    Instead of using a while loop in increment the number just use the current unix timestamp. It would be nearly impossible to have duplicates using that method. <?php echo $filename.'_'.time().'.extension';?> – Terry Carter Mar 14 '16 at 15:40
0

Just get the filename of your save() function, then split the name into two halves on the first occurring dot.

$img_name = $file['name'];  
$img = explode(".", $img_name, 2);  
$file['name'] = $img[0].'_'.rand().'.'.$img[1]; 
0

To make unique file name of uploaded file, you can concatenate file name with current time-stamp in save() function in your component. For example:

$fileName = $fileName . '_' . time();

Hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.