Phoca
Phoca Seal

Unable to upload files withe same name twice?

Unable to upload files withe same name twice?

Postby avi0007 on 04 Feb 2013, 04:40

Hi All,

I have installed phoca gallery on Joomla.

When I try to upload an image with the same file name as the one that was uploaded earlier, I get an error saying file already exists.

But I need the users to be able to upload files with the same name multiple times without getting over written or giving error.

How do we upload files with the same filename multiple times?

Thank you.
avi0007
Phoca Newbie
Phoca Newbie
 
Posts: 2
Joined: 01 Feb 2013, 18:31

Google Ads


Re: Unable to upload files withe same name twice?

Postby Jan on 07 Feb 2013, 23:49

Hi, for such feature you need to customize the component (the upload part and remove the protection for overwriting files) There is no option as default to overwrite images :-(

Jan
If you find some Phoca extensions useful, please vote for them on: extensions.joomla.org
User avatar
Jan
Phoca Hero
Phoca Hero
 
Posts: 31201
Joined: 10 Nov 2007, 18:23
Location: Czech Republic

Re: Unable to upload files withe same name twice?

Postby avi0007 on 26 Feb 2013, 11:06

Hi,

The above issue is solved now. I have customized the code to rename the file so that when people upload files with the same name it gets renamed when uploaded. For example, test.jpg when uploaded again will be renamed as test_1.jpg

For this you have to do the following:

In the file, /administrator/components/com_phocagallery/libraries/phocagallery/file/fileupload.php

Add after line 17:
Code: Select all
private $file_Name;


In realsingleUpload() function, line 379 to 387,

Replace:
Code: Select all
if (JFile::exists($filepath)) {
            if ($return) {
               $app->redirect(base64_decode($return).'&folder='.$folderUrl, JText::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'), 'error');
               exit;
            } else {
               $app->redirect($componentUrl, JText::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'), 'error');
               exit;
            }
         }


With:
Code: Select all
if (JFile::exists($filepath)) {
            
            // CHeck if there is another file version using the _ format
            //CUSTOM CODE STARTS
            $f = $file['name'];
            $filename = explode(".",$f);
            $filename = $filename[0];
            $ext = explode(".",$f);
            $ext = $ext[1];
            
            for($m = 1; $m <= 100 ; $m++){
               $this->file_Name = $filename."_".$m.".".$ext;
               $filepath1 = JPath::clean($path->image_abs.$folder.strtolower($this->file_Name));
               if(!JFile::exists($filepath1)){
                  $this->file_Name = $filename."_".($m).".".$ext;
                  $filepath1 = JPath::clean($path->image_abs.$folder.strtolower($this->file_Name));
                  
                  break;
               }
               else{
                  $mcount = explode("_",$this->file_Name);
                  $mcount = $mcount[1];
                  if(!empty($mcount)){
                     $count1 = $mcount[1];
                     $count = explode(".",$count1);
                     $count = $count[0];
                     $m = $m+$count;
                  }else{
                     $count = 1;
                     $m = $m+$count;
                  }
               }
            }
            $filepath = $filepath1;
         }
         else{
            $this->file_Name = $file['name'];   
         }



Replace:
Code: Select all
if ((int)$frontEnd > 0) {
               return $file['name'];
            }


With:
Code: Select all
if ((int)$frontEnd > 0) {
               return $this->file_Name;
            }


This works and renames any duplicate filename with _1, _2 and on.

Thank you.

Avi
avi0007
Phoca Newbie
Phoca Newbie
 
Posts: 2
Joined: 01 Feb 2013, 18:31

Re: Unable to upload files withe same name twice?

Postby Jan on 28 Feb 2013, 19:19

Hi, thank you for the guide.
Jan
If you find some Phoca extensions useful, please vote for them on: extensions.joomla.org
User avatar
Jan
Phoca Hero
Phoca Hero
 
Posts: 31201
Joined: 10 Nov 2007, 18:23
Location: Czech Republic

Re: Unable to upload files withe same name twice?

Postby Darkn3ss on 03 Jul 2013, 17:40

Your solution works very well and thank you very much but if I use multiple upload doesn't work.
Any Idea?
Darkn3ss
Phoca Newbie
Phoca Newbie
 
Posts: 2
Joined: 22 Apr 2013, 17:07


Return to General Forum

Who is online

Users browsing this forum: No registered users and 1 guest