Log in

View Full Version : saving images..



nikomou
02-03-2007, 10:51 AM
Hi,

I've got a database with a load of image urls.. i want to run a script that goes through the database, saving all images on my server.. and run this daily (using cron). If the image already exsists, it shouldnt save it again.. is this possible?

Cheers.

djr33
02-05-2007, 09:24 PM
Run through the database. Use SELECT statements, probably.
Do a loop.
Use fopen(), fwrite(), fclose() loops to save data to the image files.
Use an if with md5 of the file to see if it exists.
Using [md5].jpg as the filename for each image is quite helpful for that.
If not, store the md5 of stored images in the database. And if even that isn't possible you could run through all the images with a PHP readdir() loop then use md5file() on them to check.

nikomou
02-11-2007, 06:36 PM
cheers!

Do you have any code i could have a play about with? i dont have a clue how to start!

djr33
02-12-2007, 12:08 AM
The info is up there.

For details--
http://php-mysql-tutorial.com <<good intro to databases. All you need with SELECT and such will certainly be there.
http://www.php.net <<search in the 'function search' field and find any function, like fopen() etc.

That should be all you need.

md5 is a hash generating algorithm, meaning that when you input ANYTHING you get a result something like this--9e107d9d372bb6826bd81d3542a419d6.
Same length, varied characters, always 0-9;a-f
Using that on an image file will allow you to see if it matches what you already have.


Also, running once a day isn't something you can control with PHP. Either run it manually or try something l ike setting a chron job to automatically run it. But I wouldn't know how to help you there.

nikomou
02-19-2007, 11:37 AM
hey,

I've managed to save 1 image at a time, but i want to be able to save them all at once, using sum sort of a loop!


<?php
$file = 'http://www.affiliatesite.com/images/thisimage.jpg';
$destination = 'thisimage.jpg'; // change this for relative path to your desired server location

// retrieve & save the file
$fp = fopen($file,"r");
$fp2 = fopen("$destination", "w");
while (!feof($fp)) {
$buf = fread($fp, 1024);
fwrite($fp2, $buf);
}
fclose($fp);
fclose($fp2);
?>

djr33
02-20-2007, 06:30 AM
If you're using mysql, then the returned query can be used in a loop.


$query = 'SELECT * FROM `images`';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
dostuff();
}

Where dostuff(); is just put your function.
So... $row['filename'] is the file... then just use the same loop you have above. Etc.

Note that the reason this works, as it's confusing at first if you don't understand why, is that it literally means "While (this_is_true)", and a statement is true if it 'works'. So.... while the $row is set to a value of mysql_fetch_array, keep going. mysql_fetch_array gets an array from the results, one line per rotation. It automatically advances each time it's called. This is basic mysql usage, but confusing for someone who hasn't seen it before.

Just play with php and mysql more and the specifics will be easy after you understand the general stuff.

codeexploiter
02-20-2007, 07:29 AM
<?
class database
{
private $db_handle;
private $user_name;
private $password;
private $data_base;
private $host_name;
private $sql;
private $results;

function __construct($host="localhost",$user,$passwd)
{
$this->db_handle = mysql_connect($host,$user,$passwd);
}

function dbSelect($db)
{
$this->data_base = $db;
if(!mysql_select_db($this->data_base, $this->db_handle))
{
error_log(mysql_error(), 3, "/phplog.err");
die("Error connecting to Database");
}
}

function executeSql($sql_stmt)
{
$this->sql = $sql_stmt;
$this->result = mysql_query($this->sql);
}

function fileSaving()
{
while($record = mysql_fetch_object($this->result))
{
$fileToBCopied = $record->imageURL;

$fileArray = explode("/",$fileToBCopied);

$arrayLength = count($fileArray);

$newFileName = $fileArray[$arrayLength-1];
$fpRead = fopen($fileToBCopied,"r");
if(file_exists("C:\\images\\$newFileName"))
{
continue;
}
$fpWrite = fopen("C:\\images\\$newFileName", "w");

$buf = file_get_contents($fileToBCopied);
fwrite($fpWrite, $buf);
}
echo "<strong>File copying operation has been over</strong>";
}
}

$db = new database("localhost","root","");
$db-> dbSelect("images");

$sql = "SELECT imageURL FROM image";
$db->executeSql($sql);
$db->fileSaving();
?>


Try the above code and at the time of database object instantiation pass your database user details and password here i've used user root and an empty password.

The red color line is used to specify your database here its name is images.

It worked for me to achieve what you've mentioned in your posting.

let me know if you have any difficulty with the above code.

nikomou
02-20-2007, 06:03 PM
cheers codeexploiter!

Fantastic script - works great!!

Would this script overight files already there? (if they have the same file name?)

and if i run it again, would it skip the files already on my server, unless it has changed on the affiliates server?

Thanks again

codeexploiter
02-21-2007, 03:29 AM
Would this script overight files already there? (if they have the same file name?)
and if i run it again, would it skip the files already on my server, unless it has changed on the affiliates server?

This script is developed in such a way that it will check the whether the file to be copied is (i mean the name) exist in the images folder in your harddrive, if it exists there then that file won't be copied/saved into your harddrive.

If you want to overwrite an already existing file that won't be much difficult. By commenting the following lines in the script



if(file_exists("C:\\images\\$newFileName"))
{
continue;
}

Once you comment the above line of code it will start overwrite the files.