1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: inhere
5+ * Date: 2017-03-31
6+ * Time: 19:08
7+ */
8+
9+ namespace inhere \console \utils ;
10+
11+ /**
12+ * Class Download
13+ * @package inhere\console\utils
14+ */
15+ class Download
16+ {
17+ /*
18+ OUT:
19+ Connected...
20+ Mime-type: text/html; charset=utf-8
21+ Being redirected to: http://no2.php.net/distributions/php-5.2.5.tar.bz2
22+ Connected...
23+ Filesize: 7773024
24+ Mime-type: application/octet-stream
25+ [========================================> ] 40% (3076/7590 kb)
26+ */
27+
28+ /**
29+ * @param $code
30+ * @param $severity
31+ * @param $message
32+ * @param $messageCode
33+ * @param $transferredBytes
34+ * @param $maxBytes
35+ */
36+ public static function stream_notification_callback ($ code , $ severity , $ message , $ messageCode , $ transferredBytes , $ maxBytes )
37+ {
38+ static $ fileSize = null ;
39+
40+ switch ($ code ) {
41+ case STREAM_NOTIFY_RESOLVE :
42+ case STREAM_NOTIFY_AUTH_REQUIRED :
43+ case STREAM_NOTIFY_COMPLETED :
44+ case STREAM_NOTIFY_FAILURE :
45+ case STREAM_NOTIFY_AUTH_RESULT :
46+ /* Ignore */
47+ break ;
48+
49+ case STREAM_NOTIFY_REDIRECTED :
50+ echo "Being redirected to: " , $ message , "\n" ;
51+ break ;
52+
53+ case STREAM_NOTIFY_CONNECT :
54+ echo "Connected... \n" ;
55+ break ;
56+
57+ case STREAM_NOTIFY_FILE_SIZE_IS :
58+ $ fileSize = $ maxBytes ;
59+ echo "FileSize: " , $ fileSize , "\n" ;
60+ break ;
61+
62+ case STREAM_NOTIFY_MIME_TYPE_IS :
63+ echo "Mime-type: " , $ message , "\n" ;
64+ break ;
65+
66+ case STREAM_NOTIFY_PROGRESS :
67+ if ($ transferredBytes > 0 ) {
68+ if (!isset ($ fileSize )) {
69+ printf ("\rUnknown fileSize.. %2d kb done.. " , $ transferredBytes /1024 );
70+ } else {
71+ $ length = (int )(($ transferredBytes /$ fileSize )*100 );
72+ printf ("\r[%-100s] %d%% (%2d/%2d kb) " , str_repeat ("= " , $ length ). "> " , $ length , ($ transferredBytes /1024 ), $ fileSize /1024 );
73+ }
74+ }
75+
76+ break ;
77+ }
78+ }
79+
80+ /*
81+ OUT:
82+ Connected...
83+ Found the mime-type: text/html; charset=utf-8
84+ Being redirected to: http://no.php.net/contact
85+ Connected...
86+ Got the fileSize: 0
87+ Found the mime-type: text/html; charset=utf-8
88+ Being redirected to: http://no.php.net/contact.php
89+ Connected...
90+ Got the fileSize: 4589
91+ Found the mime-type: text/html;charset=utf-8
92+ Made some progress, downloaded 0 so far
93+ Made some progress, downloaded 0 so far
94+ Made some progress, downloaded 0 so far
95+ Made some progress, downloaded 1440 so far
96+ ... ...
97+ */
98+ public static function stream_notification_callback1 ($ code , $ severity , $ message , $ messageCode , $ transferredBytes , $ maxBytes )
99+ {
100+ switch ($ code ) {
101+ case STREAM_NOTIFY_RESOLVE :
102+ case STREAM_NOTIFY_AUTH_REQUIRED :
103+ case STREAM_NOTIFY_COMPLETED :
104+ case STREAM_NOTIFY_FAILURE :
105+ case STREAM_NOTIFY_AUTH_RESULT :
106+ var_dump ($ code , $ severity , $ message , $ messageCode , $ transferredBytes , $ maxBytes );
107+ /* Ignore */
108+ break ;
109+
110+ case STREAM_NOTIFY_REDIRECTED :
111+ echo "Being redirected to: " , $ message ;
112+ break ;
113+
114+ case STREAM_NOTIFY_CONNECT :
115+ echo "Connected... " ;
116+ break ;
117+
118+ case STREAM_NOTIFY_FILE_SIZE_IS :
119+ echo "Got the fileSize: " , $ maxBytes ;
120+ break ;
121+
122+ case STREAM_NOTIFY_MIME_TYPE_IS :
123+ echo "Found the mime-type: " , $ message ;
124+ break ;
125+
126+ case STREAM_NOTIFY_PROGRESS :
127+ echo "Made some progress, downloaded " , $ transferredBytes , " so far " ;
128+ break ;
129+ }
130+ echo "\n" ;
131+ }
132+
133+ // php down.php <http://example.com/file> <localFile>
134+ public static function down ($ url , $ saveTo , $ type = 1 )
135+ {
136+ $ ctx = stream_context_create ();
137+ stream_context_set_params ($ ctx , [
138+ "notification " => "stream_notification_callback "
139+ ]);
140+
141+ $ fp = fopen ($ url , "r " , false , $ ctx );
142+
143+ if (is_resource ($ fp ) && file_put_contents ($ saveTo , $ fp )) {
144+ echo "\nDone! \n" ;
145+ exit (0 );
146+ }
147+
148+ $ err = error_get_last ();
149+ echo "\nErrrrrorr... \n" , $ err ["message " ], "\n" ;
150+ exit (1 );
151+ }
152+
153+ }
0 commit comments