Example #1
0
 function addLogo(Brand $Brand)
 {
     if (isset($_FILES['fileLogoURL']) && $_FILES['fileLogoURL']['size'] > 0) {
         $errors = array();
         $fileName = $_FILES['fileLogoURL']['name'];
         $tmpName = $_FILES['fileLogoURL']['tmp_name'];
         $fileSize = $_FILES['fileLogoURL']['size'];
         $fileType = $_FILES['fileLogoURL']['type'];
         $File = new File($fileName, $tmpName, $fileSize, $fileType);
         if ($fileSize > 2097152) {
             $errors[] = 'File phải nhỏ hơn 2 MB';
         }
         if (!$File->isImageType()) {
             $errors[] = "";
         }
         if (empty($errors) == true) {
             //remove Logo old
             $logo_Old = trim($Brand->getLogoURL(), '"');
             if (file_exists($logo_Old)) {
                 unlink($logo_Old);
             }
             //Add logo new
             $path = '../assets/images/logoBrands/' . $Brand->getBraID();
             if (!file_exists($path)) {
                 // neu k ton tai duong dan thu muc cua id nay thi tạo mới
                 File::createDirectory($path);
             }
             $type = explode("/", $File->getFileType())[1];
             $find = array(" ", "\\", "/", ":", "*", "?", "\"", "<", ">", "|");
             $name = File::utf8convert(str_replace($find, '', $Brand->getBraName()));
             $pathNew = $path . '/logo_' . $name . "." . $type;
             $File->moveFile($pathNew);
             $Brand->setLogoURL($pathNew);
             $Brand->updateLogo();
         } else {
             //print_r($errors);
         }
         if (empty($error)) {
             //echo "Success";
         }
     }
 }
Example #2
0
 function addListImages(Product $Product)
 {
     $errors = array();
     $listImage_remove = array();
     $i = 1;
     $path = '../assets/images/productImages/' . $Product->getProID();
     if (isset($_POST["txtListImage_remove"]) && !empty($_POST["txtListImage_remove"])) {
         $listImage_remove = explode(',', $_POST["txtListImage_remove"]);
         foreach ($listImage_remove as $key => $fileName_remove) {
             $path_deleteFile = $path . '/' . $fileName_remove;
             if (file_exists($path_deleteFile)) {
                 unlink($path_deleteFile);
                 array_splice($listImage_remove, $key, 1);
             }
         }
     }
     if (isset($_FILES['listImageFiles']) && $_FILES['listImageFiles']['size'] > 0) {
         foreach ($_FILES['listImageFiles']['tmp_name'] as $key => $tmp_name) {
             $insertImage = true;
             $fileName = $_FILES['listImageFiles']['name'][$key];
             $fileSize = $_FILES['listImageFiles']['size'][$key];
             $tmpName = $_FILES['listImageFiles']['tmp_name'][$key];
             $fileType = $_FILES['listImageFiles']['type'][$key];
             foreach ($listImage_remove as $fileName_remove) {
                 if ($fileName == $fileName_remove) {
                     $insertImage = false;
                     break;
                 }
             }
             if ($insertImage) {
                 $File = new File($fileName, $tmpName, $fileSize, $fileType);
                 if ($fileSize > 2097152) {
                     $errors[] = 'File phải nhỏ hơn 2 MB';
                 }
                 if (!$File->isImageType()) {
                     $errors[] = ".";
                 }
                 if (empty($errors) == true) {
                     if (!file_exists($path)) {
                         File::createDirectory($path);
                     }
                     $type = explode("/", $File->getFileType())[1];
                     $find = array(" ", "\\", "/", ":", "*", "?", "\"", "<", ">", "|");
                     $name = File::utf8convert(str_replace($find, '', $Product->getProName()));
                     $name = $pathNew = $path . '/' . $name . '_' . $i++;
                     foreach (glob("{$path}/*") as $file) {
                         $file = substr($file, 0, strrpos($file, '.'));
                         if ($file == $pathNew) {
                             $pathNew = $path . '/' . $name . '_' . $i++;
                         }
                     }
                     $pathNew .= '.' . $type;
                     $File->moveFile($pathNew);
                 } else {
                     //print_r($errors);
                 }
             }
         }
         if (empty($error)) {
         }
     }
 }