curl 是非常好用的工具
善用它可以做到抓取遠端的圖片儲存於本地端
if (strpos($tag->getAttribute('src'), "http://") !== false) { $urlImages[] = $tag->getAttribute('src'); $path_parts = pathinfo($tag->getAttribute('src')); $ext = $path_parts['extension']; $FileName = date("Ymd") . "_" . substr(md5(uniqid(rand())), 0, 5) . "." . $ext; $localFileName = BlogPhotoPath . $insert_no . DIRECTORY_SEPARATOR . $FileName; $localUrlImages[] = BlogPhotoURL . $insert_no . '/' . $FileName; $ch = curl_init($tag->getAttribute('src')); $fp = fopen($localFileName, 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); }