[SOLVED] How rename file uploaded using example in File upload element?

F.schettino

Italian
I need rename file uploaded using File upload element.
I need modify only the name (as "surname_name_id"), not the path and not the extension.

I red
PHP:
// use Joomla file utils
jimport( 'joomla.filesystem.file' );
// get the original file and split it into parts
$old = ($formModel->formData['file']);
$oldParts = pathinfo($old);
// in this example we're using the id as the new filename
$sid = $formModel->formData['id'];
//create the new file (path and name)
$new = $oldParts['dirname'].'/'.$sid.'.'.$oldParts['extension'];
// update the file itself
JFile::move(JPATH_SITE.$old, JPATH_SITE.$new);
// update the data
$formModel->updateFormData('file', $new, true);
// update the db
$object = new stdClass();
// Must be a valid primary key value.
$object->id = $sid;
// new path + name
$object->file = $new;
// Update the data in the db
$result = JFactory::getDbo()->updateObject('ep_submission', $object, 'id');

but I don't understand what I have to personalize in it.

'joomla.filesystem.file' 'file' 'ep_submission' are fixed or have I to personalize? How? I mean: what is their meaning?

How define the new filename as "surname_name_id" (same path and extension)? And how read filename, "surname", "name", "id" in the submitted form?

Have I to personalize something else?
 
I used this code:
PHP:
/*  Estrazione dati dal database  */
$id_nominativo = $formModel->formData[id];
 
$db = JFactory::getDBO();
$query = "Select Cognome, Nome, Curriculum FROM fabrik_nominativi WHERE id=" . $id_nominativo;
$db->setQuery($query);
$riga = $db->loadAssoc();
 
$Cognome = $riga[Cognome];
$Nome = $riga[Nome];
$File_Curriculum_old = $riga[Curriculum];
 
/* Costruzione nuovo nome */
$File_Curriculum_new = dirname($File_Curriculum_old) . "/";
$File_Curriculum_new .= $Cognome;
$File_Curriculum_new .= '_' . $Nome;
$File_Curriculum_new .= '_' . $id_nominativo;
 
/*
echo "<pre>"; print_r($query . " " . $File_Curriculum_old . " " . $File_Curriculum_new);exit;
*/
 
/* Rinomina file */
rename($File_Curriculum_old,$File_Curriculum_new);
 
/* Aggiornamento in database  */
$sql = "UPDATE fabrik_nominativi SET Curriculum='" . $File_Curriculum_new . "' WHERE id=" . $id_nominativo;
mysqli_query($conn, $sql);

But
PHP:
rename($File_Curriculum_old,$File_Curriculum_new);

and (I think consequently)

PHP:
$sql = "UPDATE fabrik_nominativi SET Curriculum='" . $File_Curriculum_new . "' WHERE id=" . $id_nominativo;
mysqli_query($conn, $sql);

don't work.

Can someone tell me why and how have I to write?
 
I use it in
myForm/Plug-ins/PHP plugin - End of form submission (OnAfterProcess)
I don't use in fileupload because I cannot be sure that the form will be sent.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top