Moved urlExist to file helper class.

This commit is contained in:
2022-06-11 19:11:00 +02:00
parent 9f04566c81
commit 9e4a6e0c53
9 changed files with 74 additions and 96 deletions

View File

@ -627,6 +627,22 @@ trait Utilities
return FileHelper::getPath($type, $target, $fileType, $key, $default, $createIfNotSet);
}
/**
* Check if file exist
*
* @param string $path The url/path to check
*
* @return bool If exist true
*
* @since 3.0.9
*
* @deprecated 4.0 - Use FileHelper::exists($path);
*/
public static function urlExists($path)
{
return FileHelper::exists($path);
}
/**
* Set the component option
*

View File

@ -312,6 +312,58 @@ abstract class FileHelper
// sanitize the path
return '/' . trim( $filePath, '/' ) . '/' . $fileName;
}
/**
* Check if file exist
*
* @param string $path The url/path to check
*
* @return bool If exist true
*
* @since 3.0.9
*/
public static function exists($path)
{
$exists = false;
// if this is a local path
if (strpos($path, 'http:') === false && strpos($path, 'https:') === false)
{
if (file_exists($path))
{
$exists = true;
}
}
// check if we can use curl
elseif (function_exists('curl_version'))
{
// initiate curl
$ch = curl_init($path);
// CURLOPT_NOBODY (do not return body)
curl_setopt($ch, CURLOPT_NOBODY, true);
// make call
$result = curl_exec($ch);
// check return value
if ($result !== false)
{
// get the http CODE
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode !== 404)
{
$exists = true;
}
}
// close the connection
curl_close($ch);
}
elseif ($headers = @get_headers($path))
{
if(isset($headers[0]) && is_string($headers[0]) && strpos($headers[0],'404') === false)
{
$exists = true;
}
}
return $exists;
}
}