Results 1 to 9 of 9

Thread: saving images..

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default saving images..

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    cheers!

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

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
    <?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($fp1024); 
        
    fwrite($fp2$buf); 

    fclose($fp); 
    fclose($fp2); 
    ?>

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If you're using mysql, then the returned query can be used in a loop.

    PHP Code:
    $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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    PHP Code:
    <?
    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>";
        }
    }

    [
    COLOR="Blue"]$db = new database("localhost","root","");[/COLOR]
    [
    COLOR="Red"]$db-> dbSelect("images");[/COLOR]

    $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.

  8. #8
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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

  9. #9
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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

    Code:
    if(file_exists("C:\\images\\$newFileName"))
                {
                    continue;
                }
    Once you comment the above line of code it will start overwrite the files.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •