Forums

How to accept uploading files WITHOUT extension?

artsapiens 13 Nov, 2019
I can't figure out how to do this - if there is no extension, I cannot add it to acceptable filetypes. Forum search didn't help.
GreyHead 14 Nov, 2019
Hi artsapiens,

Don't accept them, it's a major security risk to your site that you should not take.

Bob
artsapiens 14 Nov, 2019
The problem is that some smartphones (some older models I suppose) when trying to upload photos, slice out image extension.

Maybe there is possibility just to voluntarily ADD some extension (.jpg for example) during upload if uploading file doesn't have one?

Thanks in advance, Bob!
GreyHead 14 Nov, 2019
Hi artsapiens,

You can use Custom PHP to check the files before the Upload Files action and add jpg if there is no extension set. Check the Joomla JFiles class for the code to use.

Bob
dwirianto 24 Nov, 2019
Maybe you want to use:
getExt(string $file) : string
in your custom code to get the extension of a file name. See more at Joomla API Index
artsapiens 24 Nov, 2019
I'm trying to use jfile, but it doesn't seem to solve the problem.

The code I try:[pre]jimport('joomla.filesystem.file');

$jinput = JFactory::getApplication()->input;
$file = $jinput->files->get('file_up'); /* this is file field name */

$info = pathinfo($this->data[file_up]);

if (!$info["extension"]) {
$this->data[file_up] .= "_added_EXT.jpg";
$this->data[file_data_before_change] = $file;
$file['name'] .= "_added_EXT.jpg";
$file['type'] = "image/jpeg"; [br]$this->data[file_data_after_change] = $file;[br]}[/pre]
(the code isinside Custom Php BEFORE Validate Fields part)
and debugger output is:
...
[file_up] => testfile_added_EXT.jpg
...
[file_data_before_change] => Array
(
[name] => testfile
[type] => application/octet-stream
[tmp_name] => /data02/***/tmp/phpqdevMO
[error] => 0
[size] => 594182[br] )[br][br] [file_data_after_change] => Array[br] ([br] [name] => testfile_added_EXT.jpg[br] [type] => image/jpeg[br] [tmp_name] => /data02/***/tmp/phpqdevMO[br] [error] => 0[br] [size] => 594182[br] )[br]...
as you can see, tmp file name (including extension) is changed, and even filetype changed as well, but I still getting "File extension is not permitted" error, and file is not uploaded.

Any suggestions?
artsapiens 24 Nov, 2019

in your custom code to get the extension of a file name.


You're not getting the problem. I'm trying to deal with files WITHOUT any extension.
GreyHead 24 Nov, 2019
Hi artsapiens,

I think that you need to rename the files in the PHP temporary upload folder before the CF Files Upload action

Bob
artsapiens 24 Nov, 2019
Hi Bob, this is exactly what I'm doing, I'm operating over php temp files (see file path / tmp_name in Debugger on previous post), and it happens BEFORE CF validation and upload (see screenshot attached)
GreyHead 24 Nov, 2019
Hi artsapiens,

I think that your code is changing the name in the form data - but that will be overwritten by the Upload Files action - it needs to be renamed in the temporary folder so that CF loads the already renamed file.

Bob
artsapiens 25 Nov, 2019
Hi Bob,
I experimented on localhost to be able to check what's going on in /tmp directory.
So, what I see, is the file, which is mentioned as [tmp_name] in debugger output:
[tmp_name] => /data02/***/tmp/phpqdevMO

never has any extension, whatever I upload - is it any file with extension or without - this temporary file has no extension, see below:

1 - file without extension:[pre] [file_data_before_change] => Array
(
[name] => [mark]tmp-img-no-ext[/mark][br] [type] => application/octet-stream[br] [tmp_name] => /Applications/MAMP/tmp/php/[strong][mark]phpbUO4CT[/mark][/strong][br] [error] => 0[br] [size] => 34357[br] )[/pre]

2 - file WITH extension:
    [file_data_before_change] => Array
(
[name] => tmp-img.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpD9qFqz
[error] => 0
[size] => 34357[br] )
I opened that folder in Finder and I see these files blinking, appearing for a split second and then disappear immediately.

I don't think that adding extension to this temporary file is the solution. It's not intended to have any.
artsapiens 25 Nov, 2019
Answer
ok, here is the solution that worked for me:

add php code before validation:
$info = pathinfo($this->data[file_up]);      /* necessary for finding file extension*/
if (!$info["extension"]) { /* if no extensin found */
$_FILES[file_up][name] .= "_added_EXT.jpg"; /* add some text to filename and .jpg extension */
}
where file_up is the name of your file field in your form.
GreyHead 25 Nov, 2019
Hi artsapiens,

Please see this FAQ which has some code written for a related problem. I think you should be able to adapt it to set an extension if there isn't one.

Bob
artsapiens 25 Nov, 2019
Hi Bob,

key things there happens:
      $name = "{$name}_{$i}.{$ext}";
      $_FILES[$k]['name'] = $name;

which is pretty much the same what is my code doing.

Thanks though!
This topic is locked and no more replies can be posted.