Bonjour

Je récupère le content-lengh avec CURL

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.fr");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo "\ndownload_content_length => ".$info['download_content_length']."\n";
Par contre le content-lenght est faux si il y a une redirection 302
par ex sur ce fichier
http://cdimage.debian.org/debian-cd/...md64-DVD-1.iso

Pour ça j'utilise cette fcontion
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
	function getSizeRemoteFile($url) {
    if (substr($url,0,4)=='http') {
        $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
        if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; }
        else { $x = $x['content-length']; }
    }
    else { $x = @filesize($url); }
 
    return $x;
	} 
 
	echo "\n\n".$contentlenght = getSizeRemoteFile($link);
Comment puis je faire pour utiliser cette fonction dans la fonction callback de CURL

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
file_put_contents( 'progress.txt', '' );
 
$targetFile = fopen( 'testfile.iso', 'w' );
 
$ch = curl_init( 'http://ftp.free.org/mirrors/releases.ubuntu-fr.org/11.04/ubuntu-11.04-desktop-i386-fr.iso' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
curl_setopt( $ch, CURLOPT_FILE, $targetFile );
curl_exec( $ch );
fclose( $ch );
 
function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size )
{
static $previousProgress = 0;
if ( $download_size == 0 )
$progress = 0;
else
$progress = round( $downloaded_size * 100 / $download_size );
if ( $progress > $previousProgress)
{
$previousProgress = $progress;
$fp = fopen( 'progress.txt', 'a' );
fputs( $fp, "$progress\n" );
fclose( $fp );
}
}
La variable en arguments $download_size est le content-lenght qui est faux en cas de redirection 302

Merci d'avance