Comparing filenames from directory with filenames stored in database

Discussion in 'PHP' started by PoPSiCLe, Apr 3, 2009.

  1. #1
    Hey.

    I'm currently making a script for reading directories and creating thumbnails from the pictures in said folder.

    The reading, creating and viewing parts are fine, no problem. Now, I'd like to add the filenames to a database-table, so as to easier be able to add comments to the pictures. This is where I've hit a "snag", and I'm hoping for some guidance.

    The code, as of now, is as this:
    
    <?php
    error_reporting(E_ALL);
    	//here we set the path to the image folder, and the thumbnail folder - see that you don't forget about the / at the end
    	$img_folder = "images/";
    	$med_folder = "images/medium/";
    	$tn_folder = "images/thumbs/";
    	
    	//use the directory class
     	$imgs = opendir($img_folder);
    	$tns = dir($tn_folder);
    	
    	//read all files from the  directory and checks if they are images
    	while (false !== ($file = readdir($imgs))) {
    		if (eregi("jpg", $file) || eregi("png", $file) || eregi("bmp", $file)) { ?>
    			<ul id="pics">
    <?php
    				if (file_exists($tn_folder."tn_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$tn_folder."tn_".$file,120,120); 
    				}
    				if (file_exists($med_folder."med_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$med_folder."med_".$file,600,600);
    				}
    			$pic_db = mysql_query("SELECT * FROM $t_pic");
    				if ($file != $pic_db['pic_name']){
    					mysql_query("INSERT INTO $t_pic VALUES ('','$file','')") or die(mysql_error());
    				}
    ?>
    
    				<li class="left"><a href="<?php echo $domain."/".$med_folder."med_".$file; ?>" rel="lightbox"><img src="<?php echo $tn_folder."tn_".$file; ?>" alt="" /></a></li>
    			</ul>	     
    <?php		}
        }
    	closedir($imgs);
    ?>
    	
    	
    <?php
    function createthumbs($name,$filename,$new_w,$new_h)
    {
    	$system=explode(".",$name);
    	if (preg_match("/jpg|jpeg|JPG|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    	if (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}
    	$old_x=imageSX($src_img);
    	$old_y=imageSY($src_img);
    	if ($old_x > $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$old_y*($new_h/$old_x);
    	}
    	if ($old_x < $old_y) 
    	{
    		$thumb_w=$old_x*($new_w/$old_y);
    		$thumb_h=$new_h;
    	}
    	if ($old_x == $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$new_h;
    	}
    	$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
    	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    	if (preg_match("/png|PNG/",$system[1]))
    	{
    		imagepng($dst_img,$filename, 100); 
    	} else {
    		imagejpeg($dst_img,$filename, 100); 
    	}
    	imagedestroy($dst_img); 
    	imagedestroy($src_img); 
    }
    ?>
    
    PHP:
    This part is where I am at a loss:
    
    			$pic_db = mysql_query("SELECT * FROM $t_pic");
    				if ($file != $pic_db['pic_name']){
    					mysql_query("INSERT INTO $t_pic VALUES ('','$file','')") or die(mysql_error());
    				}
    
    PHP:
    - how can I compare the $file-value to any of the names stored in the database? (I would like to be able to do this in one go, but I'm guessing I'll need to run a separate loop for the update/inserts).

    Anyone have an insight into how I would compare the values taken from the directory, and then put them into the database, but only if the file does not already exist as a record in the table?
     
    PoPSiCLe, Apr 3, 2009 IP
  2. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    something like this might work.

    
    <?php
    error_reporting(E_ALL);
    	//here we set the path to the image folder, and the thumbnail folder - see that you don't forget about the / at the end
    	$img_folder = "images/";
    	$med_folder = "images/medium/";
    	$tn_folder = "images/thumbs/";
    	
    	//use the directory class
     	$imgs = opendir($img_folder);
    	$tns = dir($tn_folder);
    	$pic_db = mysql_query("SELECT `pic_name` FROM $t_pic");
    	$pics = mysql_fetch_array($pic_db);
    	
    	//read all files from the  directory and checks if they are images
    	while (false !== ($file = readdir($imgs))) {
    		if (eregi("jpg", $file) || eregi("png", $file) || eregi("bmp", $file)) { ?>
    			<ul id="pics">
    <?php
    				if (file_exists($tn_folder."tn_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$tn_folder."tn_".$file,120,120); 
    				}
    				if (file_exists($med_folder."med_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$med_folder."med_".$file,600,600);
    				}
    			
    				if (!in_array($file,$pics)){
    					mysql_query("INSERT INTO $t_pic VALUES ('','$file','')") or die(mysql_error());
    				}
    ?>
    
    				<li class="left"><a href="<?php echo $domain."/".$med_folder."med_".$file; ?>" rel="lightbox"><img src="<?php echo $tn_folder."tn_".$file; ?>" alt="" /></a></li>
    			</ul>	     
    <?php		}
        }
    	closedir($imgs);
    ?>
    	
    	
    <?php
    function createthumbs($name,$filename,$new_w,$new_h)
    {
    	$system=explode(".",$name);
    	if (preg_match("/jpg|jpeg|JPG|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    	if (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}
    	$old_x=imageSX($src_img);
    	$old_y=imageSY($src_img);
    	if ($old_x > $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$old_y*($new_h/$old_x);
    	}
    	if ($old_x < $old_y) 
    	{
    		$thumb_w=$old_x*($new_w/$old_y);
    		$thumb_h=$new_h;
    	}
    	if ($old_x == $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$new_h;
    	}
    	$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
    	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    	if (preg_match("/png|PNG/",$system[1]))
    	{
    		imagepng($dst_img,$filename, 100); 
    	} else {
    		imagejpeg($dst_img,$filename, 100); 
    	}
    	imagedestroy($dst_img); 
    	imagedestroy($src_img); 
    }
    ?>
    
    Code (markup):
     
    lfhost, Apr 3, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Unfortunately, this doesn't work - I tried it, and it adds the filenames to the database even though the file already exists.
     
    PoPSiCLe, Apr 3, 2009 IP
  4. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi,

    test what is being put in $pics as I couldnt remember off hand if it puts the array as $pics[0]['picname'] etc etc

    if so, place all values into an array like

    
    <?php
    error_reporting(E_ALL);
    	//here we set the path to the image folder, and the thumbnail folder - see that you don't forget about the / at the end
    	$img_folder = "images/";
    	$med_folder = "images/medium/";
    	$tn_folder = "images/thumbs/";
    	
    	//use the directory class
     	$imgs = opendir($img_folder);
    	$tns = dir($tn_folder);
    	$pic_db = mysql_query("SELECT `pic_name` FROM $t_pic");
    	$mypics = mysql_fetch_array($pic_db);
        $pics=array();
        foreach($mypics as $k)
        {
           $pics[] = $k
        }
    	
    	//read all files from the  directory and checks if they are images
    	while (false !== ($file = readdir($imgs))) {
    		if (eregi("jpg", $file) || eregi("png", $file) || eregi("bmp", $file)) { ?>
    			<ul id="pics">
    <?php
    				if (file_exists($tn_folder."tn_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$tn_folder."tn_".$file,120,120); 
    				}
    				if (file_exists($med_folder."med_".$file)) {}
    				else {
    				createthumbs("$img_folder$file",$med_folder."med_".$file,600,600);
    				}
    			
    				if (!in_array($file,$pics)){
    					mysql_query("INSERT INTO $t_pic VALUES ('','$file','')") or die(mysql_error());
    				}
    ?>
    
    				<li class="left"><a href="<?php echo $domain."/".$med_folder."med_".$file; ?>" rel="lightbox"><img src="<?php echo $tn_folder."tn_".$file; ?>" alt="" /></a></li>
    			</ul>	     
    <?php		}
        }
    	closedir($imgs);
    ?>
    	
    	
    <?php
    function createthumbs($name,$filename,$new_w,$new_h)
    {
    	$system=explode(".",$name);
    	if (preg_match("/jpg|jpeg|JPG|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    	if (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}
    	$old_x=imageSX($src_img);
    	$old_y=imageSY($src_img);
    	if ($old_x > $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$old_y*($new_h/$old_x);
    	}
    	if ($old_x < $old_y) 
    	{
    		$thumb_w=$old_x*($new_w/$old_y);
    		$thumb_h=$new_h;
    	}
    	if ($old_x == $old_y) 
    	{
    		$thumb_w=$new_w;
    		$thumb_h=$new_h;
    	}
    	$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
    	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    	if (preg_match("/png|PNG/",$system[1]))
    	{
    		imagepng($dst_img,$filename, 100); 
    	} else {
    		imagejpeg($dst_img,$filename, 100); 
    	}
    	imagedestroy($dst_img); 
    	imagedestroy($src_img); 
    }
    ?>
    PHP:
     
    lfhost, Apr 4, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Might test this - I've decided to maybe go about it little differently :) Might make another function to make the thumbs and add the filenames to the database, and just use the database to display the pictures. Thanks for the help though - -I will test it and see if it works. :)
     
    PoPSiCLe, Apr 4, 2009 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Nope, still does not work. I think perhaps it is because of the $file-parameter, and not the $pics-parameter, though.
     
    PoPSiCLe, Apr 4, 2009 IP
  7. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    dump to see what is in $pics using print_r
    to see if it differs from your file names as you could be storing /folder/pic-name.jpg for example.
    Your $file parameter is that of the images in your folder from the initial read.
    and $pics is the SQL array.
     
    lfhost, Apr 5, 2009 IP