× Please submit new Bug Reports on GitHub: github.com/Jensen-Technologies/component-creator-issues/issues

File field (upload)

3 Jahre 11 Monate her #9720 von Székely Dénes
File field (upload) wurde erstellt von Székely Dénes
Setting the File type of field to Unique seems to not help when I try to prevent that subsequent uploads of files with the same name to be overwritten. Despite the fact that field is set to unique I am falling in one of these situation:
a. I leave the form XML as the Component Creator generates it. This has the result of a field like this:
<field name="file" type="FileMultiple" required="true" label="COM_FILES_FORM_LBL_FILE_FILE" description="COM_FILES_FORM_DESC_FILE_FILE" hint="COM_FILES_FORM_LBL_FILE_FILE"/>
This happening because the Unique parameter automatically sets the fields to Required. This ends up with a validation problem, as described here: docs.joomla.org/File_form_field_type -> required Cannot be used with this field type. If the field is marked as required it will always fail validation regardless of whether a file has been uploaded or not. The suggested workaround is to add a filerequired attribute which can be tested in your own file handling code.
b. I edit the form XML, and remove the required="true" part. This solves the validation error - but kills the Unique thing.
Any tips to solve the problem is greatly appreciated.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

3 Jahre 10 Monate her #9723 von Székely Dénes
Székely Dénes antwortete auf File field (upload)
I decided to try to solve it another way, to add a server side validation, which checks, if the filename already exist in the database.
Here what I did:
In the form's XML file I added this:
  <fieldset addrulepath="/components/com_files/models/rules" >
And I also modified the line wich handles the file upload field:
<field name="file" type="FileMultiple" label="COM_FILES_FORM_LBL_FILE_FILE" validate="file" description="COM_FILES_FORM_DESC_FILE_FILE" hint="COM_FILES_FORM_LBL_FILE_FILE" message="YCOM_FILES_FORM_ERROR_FILE_EXIST" />
Then I created the /rules subdir in models (as above) and added a file named file.php with this function (don't paste there the non essential parts)
use \Joomla\CMS\Factory;
 
/**
 * Form Rule class for the Joomla Framework.
 */
class JFormRuleFile extends JFormRule
{
public function test(& $element, $value, $group = null, & $input = null, & $form = null)
    {
		 $db = Factory::getDbo();
		 $query = $db->getQuery(true);
		 $query
			->select($db->quoteName('file'))
			->from($db->quoteName('#__files_file'))
			->where($db->quoteName('file') . ' LIKE  ' . $db->quote($value));
		 $db->setQuery($query);
		 $db->execute();
		return ($db->getNumRows() == 0) ? true : false;
    }
}
And nothing changed, I can still upload the same file again and again, generating new entries in database and the first upload getting overwritten. What I am doing wrong??

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

3 Jahre 10 Monate her #9724 von Søren Beck Jensen
Søren Beck Jensen antwortete auf File field (upload)
When and how are files written into that table? Why are they written into the table?

I would instead solve this by looking to see if the file actually exists. You can use file_exists($fullPath) or JFile::exists($fullPath) to see if the file actually exists and then skip the database stuff all together. That way it will also work if the file is deleted or moved.

But I think the problem in your code is that you use $db->execute(); which I don't believe is meant for select statements. I would instead try the following.

$match = $db->loadResult();

But that expects just one row and there is a slim chance that you may have more than one. So also make a limit on your query to 1 row.

$query->setLimit('1');

Then you can simply return (bool) $match;

Søren Beck Jensen
Founder, Component-Creator.com
Folgende Benutzer bedankten sich: Székely Dénes

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

3 Jahre 10 Monate her #9729 von Székely Dénes
Székely Dénes antwortete auf File field (upload)
Filenames are written in the table. I added a FileMultiple type of field in the form, which handles the upload. The files are uploaded to server, in a given directory. Both are working, currently, the file is uploaded, the name is stored in the database.
I need to tweak that part to prevent upload of files with names already in use.
So, appreciate the responses, but WHERE I should add the code you are suggesting?
BTW, selecting in ComponentCreator the FileMultiple field as being Unique triggers a catch 22 situation: You cannot upload any files anymore, because selecting Unique means setting the field as mandatory, and as is stated in your docs, thie renders the field unusable. So, take it simple:
1. I need to upload a file. A single file.
2. I need to be sure, that a file with same name isn't exist already in the system.
Period. If that's working, reliably, I can sort out the rest.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

3 Jahre 10 Monate her #9730 von Székely Dénes
Székely Dénes antwortete auf File field (upload)

Period. If that's working, reliably, I can sort out the rest.

Thank you, based on your tip, I worked it out. But this should be easier in the future - take it as a feature request. If someone needs a file upload functionality, would not nbe happy if files get accidentally overwritten.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

3 Jahre 10 Monate her #9741 von Søren Beck Jensen
Søren Beck Jensen antwortete auf File field (upload)
I completely agree, but this should be solved by Joomla's file field.

Søren Beck Jensen
Founder, Component-Creator.com

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Ladezeit der Seite: 0.172 Sekunden
Powered by Kunena Forum

Wir verwenden eigene Cookies und Cookies von Drittanbietern, um Ihr Nutzererlebnis zu verbessern und Ihnen einen optimalen Service zu bieten. Wenn Sie die Website weiter nutzen, gehen wir davon aus, dass Sie mit unserer Cookie-Politik einverstanden sind.