Forums

Multi text field for milti file upload

Gugo 14 Dec, 2016
Hello.
There is a form of multiple text fields and file downloads. Implemented by the method of Multi.
[attachment=0]Multi3.jpg[/attachment]

The symbol of the counter is a container C.
[attachment=1]MultiB1.jpg[/attachment]

Conventional settings as in demo form
[attachment=2]MultiB2.jpg[/attachment]

Apply # symbol in the code of the file name did not work in any form. In php code I tried to substitute (simplified):
text#...$file_name
text[#]...$file_name
[text#]...$file_name
text{#}...$file_name
etc...
Most likely it is necessary to work with the array but do not know how to do.
Need to do that every text is inserted to the name of the corresponding file.
Upload AAA.zip, Input text "John", Result = John_AAA.zip
Upload BBB.zip, Input text "Sara", Result = Sara_BBB.zip
Upload AAA.zip, Input text "Nik", Result = Nik_AAA.zip
etc...
Help!
Gugo 14 Dec, 2016
Sorry, the first screenshot should be the
[attachment=0]MultiB3.jpg[/attachment]
GreyHead 15 Dec, 2016
1 Likes
Hi Gugo,

It might be possible to do this by using the File Name Code box in the Files Upload action. You'd need to read the values of the current file to find the current value of the index _N_ and use that you find the matching name value.

Otherwise I think that I would do this by looping through the $form->file data after the Files Upload action and renaming each of the files then. Here's an example of some PHP code used to rename a single file that will give you a starter.
<?php
jimport('joomla.filesystem.file');
// copy the entry from the 'File name in Data/Files array' setting
$data_file_name = 'cf_pdf_file';

$name = $form->files[$data_file_name]['name'];
$path = $form->files[$data_file_name]['path'];

// remove the file name from the full path
$path = str_replace($name, '', $path);
if ( !JFile::exists($path.$name) ) {
  echo 'File not found';
  return false;
}
// get the file extension
$ext = JFile::getExt($path.$name);

// create a new name here - can use $form->data
$new_name = $form->data['ID_Tienda'].date('YmdH');
$new_name .= '.'.$ext;

// rename the file
$test = JFile::move($path.$name, $path.$new_name);

// update the $form->files values
$form->files[$data_file_name]['name'] = $new_name;
$form->files[$data_file_name]['path'] = $path.$new_name;

// fix URL bug - may not be needed
$url = str_replace(JPATH_SITE.'/', JURI::root(), $path);
$form->files[$data_file_name]['link'] = $url.$new_name;
// $form->debug[] = print_r($form->files, true);
?>

I suggest that you add debugger actions before and after the Custom code so that you can see exactly what is happening.

Bob
Gugo 18 Dec, 2016
Answer
Hello everyone!
I don't know the PHP programming language. Only on the small knowledge vb.net , through trial and error, I made the necessary programming code to solve their problems.
Using the index _N_ where either does not seem possible.
View file "..\administrator\components\com_chronoforms5\chronoforms\actions\file_upload\file_upload.php" it turned out that no provision of operations with files in file-processing loop. I with tambourines, probably, danced the whole week 🙂 . It was necessary to throw and pass to the second option.
Thanks Bob GrayHead, but with your code was necessary to suffer too. For beginners in programming it is big work though looking back, i understand that everything is simple. A pity for the lost time.
Solution of the problem in the use Custom_code after the File_Uploads, as you said.
But because a lot of files, had to use a loop to process all. If the downloaded files are the same, after the process, the form may lose them. But on overall process for achieving this does not affect.
It is possible to use function array_count_values for control of quantity, but these are already question subtleties.


<?php
jimport('joomla.filesystem.file');
$arr=$form->files[file];
$Darr=$form->files[file];
foreach ($arr as $key => $value) {
$name = $arr[$key]['name'];
$path = $arr[$key]['path'];
$Jtext = $form->data[text][$key];
// remove the file name from the full path
$path = str_replace($name, '', $path);
if ( !JFile::exists($path.$name) ) {
  echo 'File not found';
  return false;
}
// get the file extension
$ext = JFile::getExt($path.$name);

// create a new name here - can use $form->data
$new_name = $Jtext."_".$name;
//$new_name .= '.'.$ext;

// rename the file
$test = JFile::copy($path.$name, $path.$new_name);

// update the $form->files values
$form->files[file][$key]['name']= $new_name;
$form->files[file][$key]['path']= $path.$new_name;

// fix URL bug - may not be needed
$url = str_replace(DS, "/", $path);
$form->files[file][$key]['link'] = $url.$new_name;
// $form->debug[] = print_r($form->files, true);
}

foreach ($Darr as $key => $value) {
$path = $Darr[$key]['path'];
if ( !JFile::exists($path) ) {
  echo 'File not found';
  return false;
}
// del the file
unlink($path);
}
?>

I thank for the help.
This topic is locked and no more replies can be posted.