35 users online (1 members and 34 guests)  


  Results 1 to 2 of 2

Related

  1. Problem in saving data    Forum: PHP Forum
    Replies: 1
  2. Help with saving    Forum: Website Scripts Forum
    Replies: 3
  3. Replies: 1
  4. saving space on a page    Forum: HTML Forum
    Replies: 1
  1. #1
    katerina's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2008
    Posts
    1

    Problem saving images

    Hi all,
    I need your help for a project.

    I am trying to insert all images from a site into a database. The problem is that my code understand only lines in format
    <img src="" ..> and not <img ... src=""> show example code of microsoft.com

    My code :
    Code:
    <html>
    <head>
    <title> Go </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
    <body>
    
    <?php
    
    if (!@mysql_connect('localhost', 'root', ''))
    {
        echo "Error:".mysql_error();
        exit;
    }
    elseif (!@mysql_select_db('db_name'))
    {
        echo "Error: ".mysql_error();
        exit;
    }
    	
      $url="http://www.microsoft.com";
      $file = file_get_contents($url);
    
    preg_match_all ( '#(?:<img )([^>]*?)(?:/?>)#is', $file, $imgtags, PREG_PATTERN_ORDER );
    
    $imgcontents = array();
    
    foreach ( (array) $imgtags[1] as $img )
    {
    	
    	preg_match_all ( '#([a-zA-Z]*?)=[\'"]([^"\']*)["\']#i', $img, $attributes, PREG_PATTERN_ORDER );
        $attrs = array();
        foreach ( (array) $attributes[1] as $key => $attr )
        {
            $attrs[$attributes[1][$key]] = $attributes[2][$key];
        }
        $imgcontents[] = $attrs;
    }
    
    
    foreach($imgcontents as $img => $key1) {
    
        $id = $img;
        
        $query = "INSERT INTO table_images (id) VALUES ('" .$id. "')";
        mysql_query($query);
    
        foreach($key1 as $imgs => $key2) {
    	
            if ($imgs == 'src') {
            
                $img = array_reverse(split('/', $key2));
                
                $key2 = $img[0];
            }
    		
            
            $query = "UPDATE table_images SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'";
            mysql_query($query);
        
        }
    }
    
    
    mysql_close(); 
    
    ?>
    
    </body>
    </html>
    Thanks a lot
    Last edited by katerina; 09-05-2008 at 12:45 PM.

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Problem saving images

    I ran the code exactly as you have it (taking out the mysql references and adding print commands), and it runs fine. It even pulls the data from the following tag:

    <img alt="" width="0" height="0" border="0" hspace="0" vspace="0" src="http://c.microsoft.com/trans_pixel.asp?source=www&TYPE=PV&P=en_us"/>

    I do recommend adding the addslashes() function to any value before inserting it into the database. Especially if you do not have control over the value (and you don't in this case)



Tags for this Thread