[php] 외부이미지 파일 사이즈 확인하기
본문
//URL of the remote file that you want to get
//the file size of.
//Create a cURL handle with the URL of
//the remote file.
$curl = curl_init($remoteFile);
//Set CURLOPT_FOLLOWLOCATION to TRUE so that our
//cURL request follows any redirects.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//We want curl_exec to return the output as a string.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Set CURLOPT_HEADER to TRUE so that cURL returns
//the header information.
curl_setopt($curl, CURLOPT_HEADER, true);
//Set CURLOPT_NOBODY to TRUE to send a HEAD request.
//This stops cURL from downloading the entire body
//of the content.
curl_setopt($curl, CURLOPT_NOBODY, true);
//Execute the request.
curl_exec($curl);
//Retrieve the size of the remote file in bytes.
$fileSize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($fileSize);
//Convert it into KB
$fileSizeKB = round($fileSize / 1024);
echo 'File is ' . $fileSizeKB . ' KB in size.';
****************************************************************************
//URL of the remote file that you want to get
//the file size of.
//Get the header response for the file in question.
$headers = get_headers($remoteFile, 1);
//Convert the array keys to lower case for the sake
//of consistency.
$headers = array_change_key_case($headers);
//Set to -1 by default.
$fileSize = -1;
//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers['content-length'])){
$fileSize = $headers['content-length'];
}
var_dump($fileSize);
추천0
관련링크
댓글목록 0
등록된 댓글이 없습니다.