Je dois renommer et enregistrer un fichier téléchargé lors de l'enregistrement d'un élément dans un composant personnalisé. Le problème est que la fonction JFile :: makeSafe semble ne pas convertir le nom de fichier comme vous pouvez le voir ci-dessous.
J'ai donc remplacé la fonction de sauvegarde dans le fichier models/test.php comme ceci:
public function save($data) {
JRequest::checkToken() or die('Invalid Token');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
$date = JFactory::getDate()->format('Y-m-d h:m:s');
if (empty($data['created'])) {
$data['created'] = $date;
}
if (!empty($data['file'])) {
$data['file'] = JFile::makeSafe($data['file']);
$src = $data['file'];
$dest = JPATH_COMPONENT_ADMINISTRATOR . '/uploads/' . $data['client_id'] . '/' . $data['id'] . '_' . $data['file'];
if (strtolower(JFile::getExt($data['file'])) == 'doc' || strtolower(JFile::getExt($data['file'])) == 'docx') {
JLog::add('File Destination: ' . $dest, JLog::DEBUG, 'com_test-save item');
if (JFile::upload($src, $dest)) {
}
}
}
JLog::add('Data to save: ' . print_r($data, TRUE), JLog::DEBUG, 'com_test-save item');
}
return parent::save($data);
}
Le journal :
2018-07-20T09:02:38+00:00 DEBUG 127.0.0.1 com_testt-saveitem Filename before JFile::makeSafe : My FILE TO UPLOAD.docx
2018-07-20T09:02:38+00:00 DEBUG 127.0.0.1 com_test-saveitem Filename AFTER JFile::makeSafe : My FILE TO UPLOAD.docx
2018-07-20T09:02:38+00:00 DEBUG 127.0.0.1 com_test-saveitem File Destination: /Projects/myProjects/joomla/administrator/components/com_test/uploads/240/2_My FILE TO UPLOAD.docx
2018-07-20T09:02:38+00:00 DEBUG 127.0.0.1 com_test-saveitem Data to save: Array
(
[id] => 2
[title] => test
[alias] => test
[catid] => 0
[client_id] => 240
[published] => 0
[created] => 2018-07-20 06:07:43
[file] => My FILE TO UPLOAD.docx
[tags] =>
)
Et lorsque je sauvegarde l'article, l'avertissement suivant s'affiche:
Warning
Warning: Failed to move file: My FILE TO UPLOAD.docx to /Projects/myProjects/joomla/administrator/components/com_test/uploads/240/2_My FILE TO UPLOAD.docx
Les modèles/forms/test.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field name="id" type="text" default="0" label="JGLOBAL_FIELD_ID_LABEL"
readonly="true" class="readonly"
description="JGLOBAL_FIELD_ID_DESC"/>
<field name="title" type="text" class="inputbox"
size="40" label="JGLOBAL_TITLE"
description="COM_TEST_FIELD_TITLE_DESC" required="true" />
<field name="alias" type="text" class="inputbox"
size="40" label="JFIELD_ALIAS_LABEL"
description="COM_TEST_FIELD_ALIAS_DESC" />
<field name="access" type="accesslevel" label="JFIELD_ACCESS_LABEL"
description="JFIELD_ACCESS_DESC" class="span12 small" size="1" />
<field name="catid" type="category" extension="com_test" class="inputbox" default="" label="COM_TEST_FIELD_CATID_LABEL"
description="COM_TEST_FIELD_CATID_DESC">
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>
<field name="client_id" type="user"
label="COM_TEST_USER"
description="COM_TEST_USER_DESC"
required="true" />
<field name="published" type="list" label="JSTATUS" description="JFIELD_PUBLISHED_DESC" class="inputbox small" size="1" default="1" >
<option value="1">JPUBLISHED</option>
<option value="0">JUNPUBLISHED</option>
<option value="2">JARCHIVED</option>
<option value="-2">JTRASHED</option>
</field>
<field
name="cache_handler" type="cachehandler" default="" label="COM_TEST_FIELD_CACHE_HANDLER_LABEL"
description="COM_TEST_FIELD_CACHE_HANDLER_DESC" filter="Word">
<field name="catid" type="category" extension="com_test"
label="JCATEGORY" description="COM_TEST_FIELD_CATEGORY_DESC"
class="inputbox"
>
</field>
</field>
<field
name="created"
type="calendar"
label="COM_CONTENT_FIELD_CREATED_LABEL"
description="COM_CONTENT_FIELD_CREATED_DESC"
translateformat="true"
showtime="true"
size="22"
filter="user_utc" />
<field name="file" type="file" label="Enter some text" description="Choose an image from your computer with maximum 100KB"
size="40" accept=".doc, .docx" />
</fieldset>
</form>
Eh bien, la méthode JFile::makeSafe()
ne fait pas la conversion que je pensais avoir faite. Je pensais que cela supprimerait également des espaces du nom de fichier.
La description dans la documentation Joomla n'est pas très claire.
La description
Rend le nom de fichier sûr à utiliser.
J'ai donc utilisé strtolower()
et str_replace()
pour modifier le nom du fichier.