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

File field (upload)

pred 4 rokmi 5 mesiacmi #9720 od Székely Dénes
File field (upload) bolo vytvorené 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.

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

pred 4 rokmi 5 mesiacmi #9723 od Székely Dénes
Odpoveď od Székely Dénes na tému 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??

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

pred 4 rokmi 5 mesiacmi #9724 od Søren Beck Jensen
Odpoveď od Søren Beck Jensen na tému 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
Nasledujúci užívateľ(ia) povedali ďakujem: Székely Dénes

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

pred 4 rokmi 5 mesiacmi #9729 od Székely Dénes
Odpoveď od Székely Dénes na tému 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.

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

pred 4 rokmi 5 mesiacmi #9730 od Székely Dénes
Odpoveď od Székely Dénes na tému 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.

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

pred 4 rokmi 5 mesiacmi #9741 od Søren Beck Jensen
Odpoveď od Søren Beck Jensen na tému File field (upload)
I completely agree, but this should be solved by Joomla's file field.

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

Prosím Prihlásiť alebo Registrácia pre zdieľanie konverzácie.

Čas vytvorenia stránky: 0.055 sekúnd
Funguje na Kunena fórum

We use cookies so that you can place orders and we can provide a better service. You can control the use of cookies at the individual browser level. If you reject cookies, you may still use our website, but your ability to use some features or areas of our website may be limited.