Skip to content
Snippets Groups Projects
Commit 07faa56d authored by Frédéric Carretero's avatar Frédéric Carretero
Browse files

putFileContentInWsDataDir() -> Ecriture dans un fichier de ws_data

parent 428095be
Branches
Tags
No related merge requests found
......@@ -1630,4 +1630,68 @@ function getFileContentInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFil
}
}
}
/**
*This method write data to a file in ws_data.
*@file vmlib/phpUtil.inc
*@param $sModule Name of the module.
*@param $sObject Name of the object.
*@param $mId Id of the current object.
*@param $sField field name (generally DB column name).
*@param $sFileName Name of the file to read.
*@param $sFileContent Data to write to the file.
*@return $sErrorMsg The error message.
*/
function putFileContentInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName, $sFileContent) {
global $properties;
$sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId;
if(!empty($sField))
$sDestDir .= "/" . $sField;
if ($properties['fileS3Uploader'] === true) {
require_once ("aws_sdk/aws-autoloader.php");
$s3 = new Aws\S3\S3Client(array(
'version'=>'latest',
'region'=> $properties['fileS3UploaderRegion'],
'profile'=> $properties['fileS3UploaderProfil'],
'debug' => false
));
$sBucket = $properties['fileS3UploaderBucket'];
$sPrefix = "";
if (strpos($sBucket, "/") > -1){
$aBucket = explode("/", $sBucket );
$sBucket = $aBucket[0];
$sPrefix = implode("/", array_slice($aBucket, 1));
}
$sServerPath = str_replace($properties["vas_home"], $sPrefix, $sDestDir);
// Suppression du slash de début de ligne (sinon création d'un répertoire vide sur S3).
if (strpos($sServerPath, '/') === 0)
$sServerPath = substr($sServerPath, 1);
// Suppression du répertoire.
try {
$aResult = $s3->putObject(array(
'Bucket' => $sBucket,
'Key' => $sServerPath . '/' . $sFileName,
'Body' => $sFileContent
));
}
catch(Aws\S3\Exception\S3Exception $e) {
writeToErrorLog($e->getMessage());
}
}
else {
// Supprime le fichier si déja existant.
$sFilePath = $sDestDir . '/' . $sFileName;
if (file_exists($sFilePath)) {
if (!unlink($sFilePath)) {
writeToErrorLog(ERROR_DELETING_FILE . $sFilePath);
return false;
}
}
// Ecrit les données dans le fichier.
if (file_put_contents($sFilePath, $sFileContent) === false) {
writeToErrorLog(ERROR_WRITING_FILE_CONTENT . $sFilePath);
return false;
}
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment