Heim >Backend-Entwicklung >PHP-Tutorial >下面这段代码错哪了
下面这段代码哪里错了?
<br><br>/**<br> * 下载远程图片<br> * @param string $url 图片的绝对url<br> * @param string $filepath 文件的完整路径(包括目录,不包括后缀名,例如/www/images/test) ,此函数会自动根据图片url和http头信息确定图片的后缀名<br> * @return mixed 下载成功返回一个描述图片信息的数组,下载失败则返回false<br> */<br> function downloadImage($url, $filepath) {<br> //服务器返回的头信息<br> $responseHeaders = array();<br> //原始图片名<br> $originalfilename = '';<br> //图片的后缀名<br> $ext = '';<br> $ch = curl_init($url);<br> //设置curl_exec返回的值包含Http头<br> curl_setopt($ch, CURLOPT_HEADER, 1);<br> //设置curl_exec返回的值包含Http内容<br> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <br> //设置抓取跳转(http 301,302)后的页面<br> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);<br> //设置最多的HTTP重定向的数量<br> curl_setopt($ch, CURLOPT_MAXREDIRS, 2);<br><br> //服务器返回的数据(包括http头信息和内容)<br> $html = curl_exec($ch);<br> //获取此次抓取的相关信息<br> $httpinfo = curl_getinfo($ch);<br> curl_close($ch);<br> if ($html !== false) {<br> //分离response的header和body,由于服务器可能使用了302跳转,所以此处需要将字符串分离为 2+跳转次数 个子串<br> $httpArr = explode("\r\n\r\n", $html, 2 + $httpinfo['redirect_count']);<br> //倒数第二段是服务器最后一次response的http头<br> $header = $httpArr[count($httpArr) - 2];<br> //倒数第一段是服务器最后一次response的内容<br> $body = $httpArr[count($httpArr) - 1];<br> $header.="\r\n";<br><br> //获取最后一次response的header信息<br> preg_match_all('/([a-z0-9-_]+):\s*([^\r\n]+)\r\n/i', $header, $matches);<br> if (!empty($matches) && count($matches) == 3 && !empty($matches[1]) && !empty($matches[1])) {<br> for ($i = 0; $i if (array_key_exists($i, $matches[2])) {<br> $responseHeaders[$matches[1][$i]] = $matches[2][$i];<br> }<br> }<br> }<br> //获取图片后缀名<br> if (0 $originalfilename = $matches[0];<br> $ext = $matches[1];<br> } else {<br> if (array_key_exists('Content-Type', $responseHeaders)) {<br> if (0 $ext = $extmatches[1];<br> }<br> }<div class="clear"> </div>