Page 1 of 2

Unable to upload files withe same name twice?

Posted: 04 Feb 2013, 04:40
by avi0007
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.

Re: Unable to upload files withe same name twice?

Posted: 07 Feb 2013, 23:49
by Jan
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

Re: Unable to upload files withe same name twice?

Posted: 26 Feb 2013, 11:06
by avi0007
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

Re: Unable to upload files withe same name twice?

Posted: 28 Feb 2013, 19:19
by Jan
Hi, thank you for the guide.
Jan

Re: Unable to upload files withe same name twice?

Posted: 03 Jul 2013, 17:40
by Darkn3ss
Your solution works very well and thank you very much but if I use multiple upload doesn't work.
Any Idea?

Re: Unable to upload files withe same name twice?

Posted: 25 Apr 2014, 16:46
by mjrumble
Is there and update or can anybody help me with an updated version of this code edit for the 4.1.0 version of Phoca Gallery.
Or if there is another way of doing this that I have missed.

Thanks in advance.

Re: Unable to upload files withe same name twice?

Posted: 25 Apr 2014, 23:57
by Jan
Hi, for now I think, there is no update of this guide, unfortunately I am not able to somehow update this guide as I have no experiences with hardware which has such limitation - so really cannot give any clue advice :-( :idea:

Jan

Re: Unable to upload files withe same name twice?

Posted: 17 Jul 2014, 07:06
by doctor88
have you found other way to upload from _ _/_ _ devices?
I tryed this method to replace in php but doesnt work, maybe has been updated fileuploaded.php at the last version of phoca gallery...
anyway, Great component
Thanks

Re: Unable to upload files withe same name twice?

Posted: 17 Apr 2015, 13:33
by touraku
This method now returns error:
Fatal error: Using $this when not in object context

Can anyone fix this?
Terry

Re: Unable to upload files withe same name twice?

Posted: 19 Apr 2015, 00:05
by Jan
Hi, you get some javascript error, so see the error console to know exactly from which part the error is comming (which file, which line, etc.)
https://www.phoca.cz/documents/16-joomla ... -conflicts
Jan