Contao\FilesModel::findByPath PHP Method

findByPath() public static method

Find a file by its path
public static findByPath ( string $path, array $arrOptions = [] ) : FilesModel | null
$path string The path
$arrOptions array An optional options array
return FilesModel | null The model or null if there is no file
    public static function findByPath($path, array $arrOptions = array())
    {
        if (strncmp($path, TL_ROOT . '/', strlen(TL_ROOT) + 1) === 0) {
            $path = substr($path, strlen(TL_ROOT) + 1);
        }
        return static::findOneBy('path', $path, $arrOptions);
    }

Usage Example

 /**
  * Compress images
  *
  * @param boolean $arrFiles File array
  */
 public function processPostUpload($arrFiles)
 {
     if (is_array($arrFiles) && $GLOBALS['TL_CONFIG']['tinypng_api_key'] != '') {
         $strUrl = 'https://api.tinypng.com/shrink';
         $strKey = $GLOBALS['TL_CONFIG']['tinypng_api_key'];
         $strAuthorization = 'Basic ' . base64_encode("api:{$strKey}");
         foreach ($arrFiles as $file) {
             $objFile = FilesModel::findByPath($file);
             if (in_array($objFile->extension, array('png', 'jpg', 'jpeg'))) {
                 $strFile = TL_ROOT . '/' . $file;
                 $objRequest = new Request();
                 $objRequest->method = 'post';
                 $objRequest->data = file_get_contents($strFile);
                 $objRequest->setHeader('Content-type', 'image/png');
                 $objRequest->setHeader('Authorization', $strAuthorization);
                 $objRequest->send($strUrl);
                 $arrResponse = json_decode($objRequest->response);
                 if ($objRequest->code == 201) {
                     file_put_contents($strFile, fopen($arrResponse->output->url, "rb", false));
                     $objFile->tstamp = time();
                     $objFile->path = $file;
                     $objFile->hash = md5_file(TL_ROOT . '/' . $file);
                     $objFile->save();
                     System::log('Compression was successful. (File: ' . $file . ')', __METHOD__, TL_FILES);
                 } else {
                     System::log('Compression failed. (' . $arrResponse->message . ') (File: ' . $file . ')', __METHOD__, TL_FILES);
                 }
             }
         }
     }
 }
All Usage Examples Of Contao\FilesModel::findByPath