1

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.

10
  • 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, 2016 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 Mar 14, 2016 at 12:36
  • check this here - stackoverflow.com/questions/12909324/…
    – Joomler
    Mar 14, 2016 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, 2016 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, 2016 at 15:40

2 Answers 2

1

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]; 
1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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