Ejemplo n.º 1
0
 /**
  * ATTENTION AU NAME DE L'INPUT
  * ==> FORM[INPUTNAME] <==
  *
  * ATTENTION A LA TECHNIQUE DE RENVOIE D'INFORMATION !M&eacute;thode priv&eacute;e
  *
  * @return void
  */
 public function upload_async()
 {
     $datas = null;
     $response = array("error" => "");
     if (!isset($_POST["form_name"]) || empty($_POST["form_name"])) {
         $response["error"] = '$_POST["form_name"] require';
         $this->response($response);
     }
     if (!isset($_POST["input_name"]) || empty($_POST["input_name"])) {
         $response["error"] = '$_POST["input_name"] require';
         $this->response($response);
     }
     $file = $_FILES[$_POST["input_name"]];
     if (!isset($file) || empty($file)) {
         $response["error"] = "Aucun fichier n'a été transmis";
     }
     if (empty($response["error"])) {
         $app = $_POST["application"];
         $path_to_form = "includes/applications/" . $app . "/modules/";
         if (isset($_POST["module"]) && !empty($_POST['module'])) {
             $path_to_form .= $_POST["module"] . "/";
         } else {
             $path_to_form .= "front/";
         }
         $form_name = $_POST["form_name"];
         $path_to_form .= "forms/form." . $form_name . ".json";
         if (!file_exists($path_to_form)) {
             $path_to_form = preg_replace("/_[0-9]+\\.json\$/", ".json", $path_to_form);
         }
         try {
             $datas = SimpleJSON::import($path_to_form);
         } catch (Exception $e) {
             $response["error"] = "Formulaire introuvable " . $path_to_form;
             $this->response($response);
         }
         if (!is_array($datas[$_POST["input_name"]])) {
             $response["error"] = "Champs cibl&eacute; introuvable";
             $this->response($response);
         }
         $input = $datas[$_POST["input_name"]];
         if ($input["tag"] != Form::TAG_UPLOAD && ($input["tag"] != "input" && $input["attributes"]["type"] != "file")) {
             $response["error"] = "Le champ ciblé n'est pas un input type 'file'";
             $this->response($response);
         }
         $fileName = "";
         if (isset($input["fileName"])) {
             $fileName = "file" . rand(0, 999999);
         }
         $folderName = Form::PATH_TO_UPLOAD_FOLDER;
         if (isset($input["folder"])) {
             $folderName .= $input["folder"];
         }
         $upload = new Upload($file, $folderName, $fileName);
         if (isset($input["resize"]) && is_array($input["resize"])) {
             $upload->resizeImage($input["resize"][0], $input["resize"][1]);
         }
         if (!$upload->isMimeType($input["fileType"])) {
             $response["error"] = "Type de fichier non-autorisé (" . $input["fileType"] . ")";
             $this->response($response);
         }
         try {
             $upload->send();
         } catch (Exception $e) {
             $response["error"] = "Upload impossible";
             $this->response($response);
         }
         if (isset($input["fileName"]) && !empty($input["fileName"])) {
             $fileName = preg_replace("/(\\{id\\})/", $upload->id_upload, $input["fileName"]);
             $upload->renameFile($fileName);
         }
         $response["path_upload"] = Application::getInstance()->getPathPart() . $upload->pathFile;
         $response["id_upload"] = $upload->id_upload;
     }
     $this->response($response);
 }
Ejemplo n.º 2
0
 /**
  * Méthode de d'exécution des uploads si le formulaire présente des inputs type FILE
  * @return void
  */
 private function performUploads()
 {
     $this->uploadsFailMimeType = array();
     $this->uploadsSendFail = array();
     $this->uploads = array();
     if (!isset($_FILES[$this->name])) {
         return;
     }
     $tmp = $_FILES[$this->name];
     $this->files = array();
     foreach ($tmp["name"] as $name => $value) {
         $this->files[$name] = array();
     }
     foreach ($this->files as $name => $value) {
         $file = array();
         $file["name"] = $tmp["name"][$name];
         $file["type"] = $tmp["type"][$name];
         $file["tmp_name"] = $tmp["tmp_name"][$name];
         $file["error"] = $tmp["error"][$name];
         $file["size"] = $tmp["size"][$name];
         $this->files[$name] = $file;
     }
     foreach ($this->files as $name => $data) {
         if (isset($this->post[$name]) && !empty($this->post[$name])) {
             continue;
         }
         $input = $this->data[$name];
         if (isset($input["fileName"])) {
             $fileName = "file" . rand(0, 999999);
         } else {
             $fileName = "";
         }
         $folder = self::PATH_TO_UPLOAD_FOLDER;
         if (isset($input["folder"])) {
             $folder .= $input["folder"];
         }
         $up = new Upload($data, $folder, $fileName);
         if (isset($input["resize"]) && is_array($input["resize"])) {
             $up->resizeImage($input["resize"][0], $input["resize"][1]);
         }
         if (!$up->isMimeType($input["fileType"])) {
             $this->uploadsFailMimeType[] = $name;
             continue;
         }
         try {
             $up->send(true);
         } catch (Exception $e) {
             $this->uploadsSendFail[] = $name;
             continue;
         }
         $this->data[$name]["isUpload"] = true;
         $this->post[$name] = $up->id_upload;
         $this->uploads[] = array("name" => $name, "instance" => $up);
     }
 }