Last active
August 29, 2015 13:56
-
-
Save Swader/9036132 to your computer and use it in GitHub Desktop.
Agbonghama Collins' code for image scraper article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ZipImages | |
{ | |
private $folder; | |
private $url; | |
private $html; | |
private $fileName; | |
private $status; | |
public function __construct($url) | |
{ | |
$this->url = $url; | |
$this->html = file_get_contents($this->url); | |
$this->setFolder(); | |
} | |
public function setFolder($folder = "image") | |
{ | |
if (!file_exists($folder)) { | |
mkdir($folder); | |
} | |
$this->folder = $folder; | |
} | |
public function setFileName($name = "zipImages") | |
{ | |
$this->fileName = $name; | |
} | |
public function domCrawler() | |
{ | |
$crawler = new Crawler($this->html); | |
$result = $crawler | |
->filterXpath('//img') | |
->extract(array('src')); | |
foreach ($result as $image) { | |
$path = $this->folder . "/" . basename($image); | |
$file = file_get_contents($image); | |
$insert = file_put_contents($path, $file); | |
if (!$insert) { | |
echo "fail to insert file to folder"; | |
exit; | |
} | |
} | |
} | |
public function createZip() | |
{ | |
$folderFiles = scandir($this->folder); | |
if (!$folderFiles) { | |
echo "fail to scan folder"; | |
exit; | |
} | |
$fileArray = array(); | |
foreach ($folderFiles as $file) { | |
if (($file != ".") | |
&& ($file != "..") | |
) { | |
$fileArray[] = $this->folder . "/" . $file; | |
} | |
} | |
if (create_zip($fileArray, $this->fileName . '.zip')) { | |
$this->status = <<<HTML | |
File successfully archived. | |
<a href="$this->fileName.zip">Download it now</a> | |
HTML; | |
} else { | |
$this->status = "An error occurred"; | |
} | |
} | |
public function deleteCreatedFolder() | |
{ | |
$dp = opendir($this->folder) | |
or die ('ERROR: Cannot open directory'); | |
while ($file = readdir($dp)) { | |
if ($file != '.' && $file != '..') { | |
if (is_file("$this->folder/$file")) { | |
unlink("$this->folder/$file"); | |
} | |
} | |
} | |
rmdir($this->folder) or die ('could not delete folder'); | |
} | |
public function getStatus() | |
{ | |
echo $this->status; | |
} | |
public function process() | |
{ | |
$this->domCrawler(); | |
$this->createZip(); | |
$this->deleteCreatedFolder(); | |
$this->getStatus(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_zip($files = array(), $destination = '', $overwrite = false) | |
{ | |
//if the zip file already exists and overwrites is false, return false | |
if (file_exists($destination) && !$overwrite) { | |
return false; | |
} | |
$valid_files = array(); | |
//if files were passed in... | |
if (is_array($files)) { | |
//cycle through each file | |
foreach ($files as $file) { | |
//make sure the file exists | |
if (file_exists($file)) { | |
$valid_files[] = $file; | |
} | |
} | |
} | |
//if we have good files... | |
if (count($valid_files)) { | |
//create the archive | |
$zip = new ZipArchive(); | |
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { | |
return false; | |
} | |
//add the files | |
foreach ($valid_files as $file) { | |
$zip->addFile($file, $file); | |
} | |
$zip->close(); | |
//check to make sure the file exists | |
return file_exists($destination); | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment