Skip to content
Snippets Groups Projects
Commit f191f1c1 authored by Armand Bahi's avatar Armand Bahi
Browse files

Fournit une structure identique à l'upload de fichier dans $_FILES

parent 629c49e2
Branches
Tags
No related merge requests found
...@@ -63,24 +63,13 @@ if (!empty($boundary)) { ...@@ -63,24 +63,13 @@ if (!empty($boundary)) {
$aFilenamePathInfo = pathinfo($filename); $aFilenamePathInfo = pathinfo($filename);
$filename = $aFilenamePathInfo['filename'] . '.' . $aFilenamePathInfo['extension']; $filename = $aFilenamePathInfo['filename'] . '.' . $aFilenamePathInfo['extension'];
// Multiples documents // [DEPRECATED]
if (substr($name, -2) == '[]') {
$_FILES[substr($name, 0, -2)][] = array(
"file" => substr($body, 0, strlen($body) - 2),
"name" => $filename
);
}
// Documents simples
else {
$_PUTDATA[$name . "_file"] = substr($body, 0, strlen($body) - 2); $_PUTDATA[$name . "_file"] = substr($body, 0, strlen($body) - 2);
$_PUTDATA[$name . "_name"] = $filename; $_PUTDATA[$name . "_name"] = $filename;
// Utilisation de $_FILES // Extrait les fichiers de aValues dans $_FILES
$_FILES[$name] = array( extractPutFileStruct($name, $filename, substr($body, 0, strlen($body) - 2), $properties);
"file" => substr($body, 0, strlen($body) - 2),
"name" => $filename
);
}
} else { } else {
$_PUTDATA[$name] = substr($body, 0, strlen($body) - 2); $_PUTDATA[$name] = substr($body, 0, strlen($body) - 2);
} }
...@@ -88,13 +77,12 @@ if (!empty($boundary)) { ...@@ -88,13 +77,12 @@ if (!empty($boundary)) {
} }
} }
error_log('$_POST: '. print_r($_POST, true));
error_log('$_FILES: '. print_r($_FILES, true));
$aParamsJson = json_decode(file_get_contents('php://input'), true); $aParamsJson = json_decode(file_get_contents('php://input'), true);
if ($_PUTDATA != null) { if ($_PUTDATA != null) {
$aValues = array_merge($_REQUEST, $_PUTDATA); $aValues = array_merge($_REQUEST, $_PUTDATA);
} else { } else {
// Réorganise $_FILES pour les fichiers multiples
extractPostFileStruct();
$aValues = $_REQUEST; $aValues = $_REQUEST;
} }
if ($aParamsJson != null) { if ($aParamsJson != null) {
...@@ -207,5 +195,69 @@ function customErrorHandler ($errno, $errstr, $errfile, $errline, $context){ ...@@ -207,5 +195,69 @@ function customErrorHandler ($errno, $errstr, $errfile, $errline, $context){
} }
} }
/**
* Extract the PUT upload files structure and put them into $_FILES
*
* @param {string} $sField Field name
* @param {string} $sFileName File name
* @param {string} $sFileContent File content
* @param {array} $properties properties
*/
function extractPutFileStruct($sField, $sFileName, $sFileContent, $properties) {
// Crée un fichier dans tmp
$sTmpFile = $properties['extract_dir'] . "/" . getUniqRandomId();
$oFile = fopen($sTmpFile, 'w+');
if (!$oFile){
writeToErrorLog("Can't open file in " . $properties['extract_dir']);
return null;
}else{
fwrite($oFile, $sFileContent);
fclose($oFile);
$aFileStruc = array(
"name" => $sFileName,
"tmp_name" => $sTmpFile,
"error" => "0",
"size" => filesize($sTmpFile)
);
}
// Fichiers simples
if (substr($sField, -2) != '[]') {
$_FILES[$sField] = $aFileStruc;
}
// Fichiers multiples
if (substr($sField, -2) == '[]') {
$_FILES[substr($sField, 0, -2)][] = $aFileStruc;
}
}
/**
* Extract the POST upload multiple files into a common structure
*
*/
function extractPostFileStruct() {
foreach ($_FILES as $key => $value) {
// Fichier multiple
if (is_array($value['name'])) {
$aFiles = [];
for ($i=0; $i < count($value['name']); $i++) {
$aFile = [];
foreach ($value as $key2 => $value2) {
$aFile[$key2] = $value[$key2][$i];
}
array_push($aFiles, $aFile);
}
$_FILES[$key] = $aFiles;
}
}
}
include ("index.vhtml"); include ("index.vhtml");
?> ?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment