क्या फ़ाइल को डाउनलोड किए बिना दूरस्थ फ़ाइल http: //my_url/my_file.txt का आकार प्राप्त करने का कोई तरीका है ?
क्या फ़ाइल को डाउनलोड किए बिना दूरस्थ फ़ाइल http: //my_url/my_file.txt का आकार प्राप्त करने का कोई तरीका है ?
जवाबों:
यहाँ इस बारे में कुछ मिला :
एक दूरस्थ फ़ाइल का आकार पाने के लिए यहां सबसे अच्छा तरीका है (जो मैंने पाया है)। ध्यान दें कि HEAD अनुरोधों को अनुरोध का वास्तविक निकाय नहीं मिलता है, वे केवल हेडर प्राप्त करते हैं। इसलिए 100MB के संसाधन के लिए HEAD अनुरोध करने से उस संसाधन के लिए HEAD अनुरोध के समान समय लगेगा जो 1KB है।
<?php
/**
* Returns the size of a file without downloading it, or -1 if the file
* size could not be determined.
*
* @param $url - The location of the remote file to download. Cannot
* be null or empty.
*
* @return The size of the file referenced by $url, or -1 if the size
* could not be determined.
*/
function curl_get_file_size( $url ) {
// Assume failure.
$result = -1;
$curl = curl_init( $url );
// Issue a HEAD request and follow any redirects.
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );
$data = curl_exec( $curl );
curl_close( $curl );
if( $data ) {
$content_length = "unknown";
$status = "unknown";
if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
$status = (int)$matches[1];
}
if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
$content_length = (int)$matches[1];
}
// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if( $status == 200 || ($status > 300 && $status <= 308) ) {
$result = $content_length;
}
}
return $result;
}
?>
उपयोग:
$file_size = curl_get_file_size( "http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file" );
curl_getinfo, जैसे @macki बताता है?
get_user_agent_string()था जैसा कि परिभाषित नहीं किया गया था। पूरी लाइन को हटाने से पूरी चीज काम कर गई।
http://www.dailymotion.com/rss/user/dialhainaut/SO देखें: stackoverflow.com/questions/36761377/…
इस कोड को आज़माएं
function retrieve_remote_file_size($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);।
CURLOPT_FOLLOWLOCATIONसच करने के लिए सेट है।
जैसा कि एक दो बार उल्लेख किया गया है, जाने का तरीका प्रतिक्रिया हैडर के Content-Lengthक्षेत्र से जानकारी प्राप्त करना है ।
हालाँकि, आपको ध्यान देना चाहिए
fopenया एक जैसे या यहाँ तक कि कर्ल पुस्तकालय आह्वान करने के लिए, जब पीएचपी है get_headers()(याद रखें: KISS )उपयोग की get_headers()इस प्रकार KISS सिद्धांत और काम करता है, भले ही आप जिस सर्वर की जांच कर रहे हैं HEAD अनुरोध का समर्थन नहीं करता।
तो, यहाँ मेरा संस्करण है (नौटंकी: मानव पठनीय स्वरूपित आकार ;-) लौटाता है):
Gist : https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d (कर्ल और get_headers संस्करण)
get_headers () - संस्करण:
<?php
/**
* Get the file size of any remote resource (using get_headers()),
* either in bytes or - default - as human-readable formatted string.
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @license MIT <http://eyecatchup.mit-license.org/>
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
*
* @param string $url Takes the remote object's URL.
* @param boolean $formatSize Whether to return size in bytes or formatted.
* @param boolean $useHead Whether to use HEAD requests. If false, uses GET.
* @return string Returns human-readable formatted size
* or size in bytes (default: formatted).
*/
function getRemoteFilesize($url, $formatSize = true, $useHead = true)
{
if (false !== $useHead) {
stream_context_set_default(array('http' => array('method' => 'HEAD')));
}
$head = array_change_key_case(get_headers($url, 1));
// content-length of download (in bytes), read from Content-Length: field
$clen = isset($head['content-length']) ? $head['content-length'] : 0;
// cannot retrieve file size, return "-1"
if (!$clen) {
return -1;
}
if (!$formatSize) {
return $clen; // return size in bytes
}
$size = $clen;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KiB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB'; break;
}
return $size; // return formatted size
}
उपयोग:
$url = 'http://download.tuxfamily.org/notepadplus/6.6.9/npp.6.6.9.Installer.exe';
echo getRemoteFilesize($url); // echoes "7.51 MiB"
अतिरिक्त नोट: सामग्री-लंबाई शीर्ष लेख वैकल्पिक है। इस प्रकार, एक सामान्य समाधान के रूप में यह बुलेट प्रूफ नहीं है !
Content-Lengthवैकल्पिक है, लेकिन इसे डाउनलोड किए बिना फ़ाइल का आकार प्राप्त करने का एकमात्र तरीका है - और इसे get_headersप्राप्त करने का सबसे अच्छा तरीका है content-length।
stream_context_createकॉल के लिए उपयोग करने के लिए एक अलग संदर्भ बनाने के लिए उपयोग करें get_headers।
Php समारोह get_headers()मेरे लिए काम करता जाँच करने के लिए सामग्री-लंबाई के रूप में
$headers = get_headers('http://example.com/image.jpg', 1);
$filesize = $headers['Content-Length'];
अधिक विस्तार के लिए: PHP फंक्शन get_headers ()
मुझे यकीन नहीं है, लेकिन क्या आप इसके लिए get_headers फ़ंक्शन का उपयोग नहीं कर सकते हैं?
$url = 'http://example.com/dir/file.txt';
$headers = get_headers($url, true);
if ( isset($headers['Content-Length']) ) {
$size = 'file size:' . $headers['Content-Length'];
}
else {
$size = 'file size: unknown';
}
echo $size;
एक पंक्ति सबसे अच्छा समाधान:
echo array_change_key_case(get_headers("http://.../file.txt",1))['content-length'];
php बहुत डेलिकियस है
function urlsize($url):int{
return array_change_key_case(get_headers($url,1))['content-length'];
}
echo urlsize("http://.../file.txt");
सबसे सरल और सबसे कुशल कार्यान्वयन:
function remote_filesize($url, $fallback_to_download = false)
{
static $regex = '/^Content-Length: *+\K\d++$/im';
if (!$fp = @fopen($url, 'rb')) {
return false;
}
if (isset($http_response_header) && preg_match($regex, implode("\n", $http_response_header), $matches)) {
return (int)$matches[0];
}
if (!$fallback_to_download) {
return false;
}
return strlen(stream_get_contents($fp));
}
Content-Lengthहेडर है। और स्पष्ट $fpसमापन आवश्यक नहीं है; यह स्वतः ही समाप्त हो जाता है। php.net/manual/en/language.types.resource.php
nc -l localhost 8080
*closeमें आधुनिक PHP में अधिकांश कार्य आवश्यक नहीं हैं। वे दो ऐतिहासिक कारणों से हैं: कार्यान्वयन प्रतिबंध और सी भाषा की नकल।
चूंकि यह प्रश्न पहले से ही "php" और "कर्ल" के रूप में चिह्नित किया गया है, इसलिए मैं आपको यह मान रहा हूं कि PHP में कर्ल का उपयोग कैसे करें।
यदि आप सेट करते हैं, curl_setopt(CURLOPT_NOBODY, TRUE)तो आप एक HEAD अनुरोध करेंगे और संभवतः प्रतिक्रिया के "सामग्री-लंबाई" हेडर की जांच कर सकते हैं, जो केवल हेडर होगा।
दूरस्थ फ़ाइल आकार प्राप्त करने के लिए नीचे दिए गए फ़ंक्शन का प्रयास करें
function remote_file_size($url){
$head = "";
$url_p = parse_url($url);
$host = $url_p["host"];
if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){
$ip=gethostbyname($host);
if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){
return -1;
}
}
if(isset($url_p["port"]))
$port = intval($url_p["port"]);
else
$port = 80;
if(!$port) $port=80;
$path = $url_p["path"];
$fp = fsockopen($host, $port, $errno, $errstr, 20);
if(!$fp) {
return false;
} else {
fputs($fp, "HEAD " . $url . " HTTP/1.1\r\n");
fputs($fp, "HOST: " . $host . "\r\n");
fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof($fp)) {
$headers .= fgets ($fp, 128);
}
}
fclose ($fp);
$return = -2;
$arr_headers = explode("\n", $headers);
foreach($arr_headers as $header) {
$s1 = "HTTP/1.1";
$s2 = "Content-Length: ";
$s3 = "Location: ";
if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size = substr($header, strlen($s2));
if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));
}
if(intval($size) > 0) {
$return=intval($size);
} else {
$return=$status;
}
if (intval($status)==302 && strlen($newurl) > 0) {
$return = remote_file_size($newurl);
}
return $return;
}
यहां एक और दृष्टिकोण है जो उन सर्वरों के साथ काम करेगा जो HEADअनुरोधों का समर्थन नहीं करते हैं।
यह फ़ाइल के पहले बाइट के लिए पूछने वाले HTTP रेंज हेडर के साथ सामग्री के लिए अनुरोध करने के लिए cURL का उपयोग करता है।
यदि सर्वर रेंज अनुरोधों का समर्थन करता है (अधिकांश मीडिया सर्वर) तो यह संसाधन के आकार के साथ प्रतिक्रिया प्राप्त करेगा।
यदि सर्वर बाइट रेंज के साथ प्रतिक्रिया नहीं करता है, तो यह लंबाई निर्धारित करने के लिए सामग्री-लंबाई हेडर के लिए दिखेगा।
यदि आकार किसी श्रेणी या सामग्री-लंबाई शीर्ष लेख में पाया जाता है, तो स्थानांतरण रद्द कर दिया जाता है। यदि आकार नहीं मिलता है और फ़ंक्शन प्रतिक्रिया निकाय पढ़ना शुरू कर देता है, तो स्थानांतरण रद्द कर दिया जाता है।
यह एक अनुपूरक तरीका हो सकता है यदि HEADअनुरोध 405विधि में समर्थित प्रतिक्रिया न हो।
/**
* Try to determine the size of a remote file by making an HTTP request for
* a byte range, or look for the content-length header in the response.
* The function aborts the transfer as soon as the size is found, or if no
* length headers are returned, it aborts the transfer.
*
* @return int|null null if size could not be determined, or length of content
*/
function getRemoteFileSize($url)
{
$ch = curl_init($url);
$headers = array(
'Range: bytes=0-1',
'Connection: close',
);
$in_headers = true;
$size = null;
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2450.0 Iron/46.0.2450.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0); // set to 1 to debug
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'r'));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $line) use (&$in_headers, &$size) {
$length = strlen($line);
if (trim($line) == '') {
$in_headers = false;
}
list($header, $content) = explode(':', $line, 2);
$header = strtolower(trim($header));
if ($header == 'content-range') {
// found a content-range header
list($rng, $s) = explode('/', $content, 2);
$size = (int)$s;
return 0; // aborts transfer
} else if ($header == 'content-length' && 206 != curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
// found content-length header and this is not a 206 Partial Content response (range response)
$size = (int)$content;
return 0;
} else {
// continue
return $length;
}
});
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $data) use ($in_headers) {
if (!$in_headers) {
// shouldn't be here unless we couldn't determine file size
// abort transfer
return 0;
}
// write function is also called when reading headers
return strlen($data);
});
$result = curl_exec($ch);
$info = curl_getinfo($ch);
return $size;
}
उपयोग:
$size = getRemoteFileSize('http://example.com/video.mp4');
if ($size === null) {
echo "Could not determine file size from headers.";
} else {
echo "File size is {$size} bytes.";
}
Content-Lengthउपलब्ध न हो।
यहाँ ज्यादातर उत्तर या तो CURL का उपयोग करते हैं या हेडर पढ़ने पर आधारित होते हैं। लेकिन कुछ निश्चित स्थितियों में आप एक आसान उपाय का उपयोग कर सकते हैं। filesize()PHP.net पर नोट पर ध्यान दें । आपको यह कहते हुए एक टिप मिलेगी: " PHP 5.0.0 के रूप में, इस फ़ंक्शन का उपयोग कुछ URL रैपर के साथ भी किया जा सकता है। समर्थित प्रोटोकॉल और रैपर्स का संदर्भ लें ताकि यह निर्धारित किया जा सके कि कौन से रैपर सपोर्ट स्टेट (कार्यक्षमता) के परिवार का समर्थन करते हैं। "।
इसलिए, यदि आपका सर्वर और PHP पार्सर ठीक से कॉन्फ़िगर किया गया है, तो आप बस filesize()फंक्शन का उपयोग कर सकते हैं , इसे पूर्ण URL के साथ खिलाया जा सकता है, एक दूरस्थ फ़ाइल की ओर इशारा करते हुए, जिसे आकार आप प्राप्त करना चाहते हैं, और PHP को सभी जादू करने दें।
इसे आज़माएं: मैंने इसका इस्तेमाल किया और अच्छा परिणाम मिला।
function getRemoteFilesize($url)
{
$file_headers = @get_headers($url, 1);
if($size =getSize($file_headers)){
return $size;
} elseif($file_headers[0] == "HTTP/1.1 302 Found"){
if (isset($file_headers["Location"])) {
$url = $file_headers["Location"][0];
if (strpos($url, "/_as/") !== false) {
$url = substr($url, 0, strpos($url, "/_as/"));
}
$file_headers = @get_headers($url, 1);
return getSize($file_headers);
}
}
return false;
}
function getSize($file_headers){
if (!$file_headers || $file_headers[0] == "HTTP/1.1 404 Not Found" || $file_headers[0] == "HTTP/1.0 404 Not Found") {
return false;
} elseif ($file_headers[0] == "HTTP/1.0 200 OK" || $file_headers[0] == "HTTP/1.1 200 OK") {
$clen=(isset($file_headers['Content-Length']))?$file_headers['Content-Length']:false;
$size = $clen;
if($clen) {
switch ($clen) {
case $clen < 1024:
$size = $clen . ' B';
break;
case $clen < 1048576:
$size = round($clen / 1024, 2) . ' KiB';
break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB';
break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB';
break;
}
}
return $size;
}
return false;
}
अब, इनका परीक्षण करें:
echo getRemoteFilesize('http://mandasoy.com/wp-content/themes/spacious/images/plain.png').PHP_EOL;
echo getRemoteFilesize('http://bookfi.net/dl/201893/e96818').PHP_EOL;
echo getRemoteFilesize('/programming/14679268/downloading-files-as-attachment-filesize-incorrect').PHP_EOL;
परिणाम:
24.82 KiB
912 कीबी
101.85 कीबी
HTTP / 2 अनुरोध को कवर करने के लिए, यहां दिए गए फ़ंक्शन https://stackoverflow.com/a/2602624/2380767 को थोड़ा बदलना होगा:
<?php
/**
* Returns the size of a file without downloading it, or -1 if the file
* size could not be determined.
*
* @param $url - The location of the remote file to download. Cannot
* be null or empty.
*
* @return The size of the file referenced by $url, or -1 if the size
* could not be determined.
*/
function curl_get_file_size( $url ) {
// Assume failure.
$result = -1;
$curl = curl_init( $url );
// Issue a HEAD request and follow any redirects.
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );
$data = curl_exec( $curl );
curl_close( $curl );
if( $data ) {
$content_length = "unknown";
$status = "unknown";
if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
$status = (int)$matches[1];
} elseif( preg_match( "/^HTTP\/2 (\d\d\d)/", $data, $matches ) ) {
$status = (int)$matches[1];
}
if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
$content_length = (int)$matches[1];
} elseif( preg_match( "/content-length: (\d+)/", $data, $matches ) ) {
$content_length = (int)$matches[1];
}
// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if( $status == 200 || ($status > 300 && $status <= 308) ) {
$result = $content_length;
}
}
return $result;
}
?>