Gallery only copy image into thumbs folder when chose upload to main folders

Discussion in 'PHP' started by macaela, Sep 2, 2010.

  1. #1
    Hi the file file upload whenever i upload an image to main directory it saves one image into photos folder and copy another into thumbs folder but whenever i upload image to a different album (not main directory) it doesnt copy the other image to thumbs folder what i'm i doing wrong

    this is the line that i chose the folder...
    function make_locations ($locs)
    
    {
    
        $html = '<select name="locations" class="input"><option value="0">-- Main Directory --</option>';
    
        foreach ($locs AS $key => $value)
    
        {
    
            $html .= '<option value="' .$value['album_id']. '">' .addslashes($value['album_name']). '</option>';
    
        }
    
        $html .= '</select>';
    
    
    
        return $html;
    
    } 
    
    PHP:
    this the whole code
    <?php
    /**
    * create a sub album directory for photos
    * to be stored in
    */
    function create_album ($album_name, $sub_album = 0)
    {
    	global $db;
    
    	$sql = "INSERT INTO " .ALBUMS_TABLE. "
    		(
    			album_name,
    			sub_album
    		)
    		VALUES
    		(
    			'" .addslashes($album_name). "',
    			" .intval($sub_album). "
    		)";
    
    	if ($db->query($sql))
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
    
    /**
    * get a list of sub albums
    */
    function get_albums ($sub_album = 0)
    {
    	global $db;
    
    	$sql = "SELECT *
    		FROM " .ALBUMS_TABLE. "
    		WHERE sub_album = " .intval($sub_album);
    
    	$db->query($sql);
    
    	// initialise album storage
    	$albums = array();
    	while ($row = $db->fetch())
    	{
    		$albums[] = $row;
    	}
    
    	return $albums;
    }
    
    /**
    * get a list of photos within an album
    */
    function get_photos ($album = 0)
    {
    	global $db;
    
    	$sql = "SELECT *
    		FROM " .PHOTOS_TABLE. "
    		WHERE album_id = " .intval($album). "
    		ORDER BY photo_id ASC";
    
    	$db->query($sql);
    
    	// initialise album storage
    	$photos = array();
    	while ($row = $db->fetch())
    	{
    		$photos[] = $row;
    	}
    
    	return $photos;
    }
    
    /**
    * get a large version of a particular photo
    */
    function get_large ($photo, $album = 0, $next = '')
    {
    	global $db, $template, $account_details;
    
    	// shall we get the next / previous photo
    	switch ($next)
    	{
    		case "next" :
    
    			$next_sql = "AND photo_id > " .intval($photo). " ORDER BY photo_id ASC";
    			break;
    
    		case "previous" :
    
    			$next_sql = "AND photo_id < " .intval($photo). " ORDER BY photo_id DESC";
    			break;
    
    		default :
    
    			$next_sql = "AND photo_id = " .intval($photo);
    			break;
    	}
    
    	$sql = "SELECT *
    		FROM " .PHOTOS_TABLE. "
    		WHERE album_id = " .intval($album). "
    		" .$next_sql. "
    		LIMIT 1";
    
    	$db->query($sql);
    	if ($db->num_rows() == 0)
    	{
    		return '';
    	}
    	else
    	{
    		$photo_info = $db->fetch();
    
    		// note the total directory
    		$location = 'photos/' .$photo_info['photo_proper'];
    
    		// now we need to resize
    		list($width, $height) = getimagesize($location);
    		if ($width < 300)
    		{
    			$new_width = $width;
    			$new_height = $height;
    		}
    		elseif ($width > $height)
    		{
    			$new_width = 300;
    			$new_height = $height * ($new_width / $width);
    		}
    		elseif ($width < $height)
    		{
    			$new_width = $width * ($new_height / $height);
    			$new_height = 300;
    		}
    
    		// work out the next link
    		$next = '<a href="portfolio.php?p=' .$photo_info['photo_id']. '&a=' .$photo_info['album_id']. '&next=next">Next</a>';
    		$previous = '<a href="portfolio.php?p=' .$photo_info['photo_id']. '&a=' .$photo_info['album_id']. '&next=previous">Previous</a>';			
    
    		return '<div id="largephoto"><img src="' .$location. '" height="' .$new_height. '" width="' .$new_width. '" class="large"><br /><div id="prevnext">' .$previous. ' :: ' .$next. '</div></div>';
    	}
    }
    
    /**
    * get an array of locations
    */
    function get_locations ()
    {
    	global $db;
    
    	// initialise array
    	$locations = array();
    
    	// select albums
    	$sql = "SELECT
    
    			album_id,
    			album_name
    
    		FROM " .ALBUMS_TABLE;
    
    	$db->query($sql);
    	while ($row = $db->fetch())
    	{
    		$locations[] = $row;
    		
    	}
    
    	return $locations;
    }
    
    /**
    * make the locations table
    */
    function make_locations ($locs)
    {
    	$html = '<select name="locations" class="input"><option value="0">-- Main Directory --</option>';
    	foreach ($locs AS $key => $value)
    	{
    		$html .= '<option value="' .$value['album_id']. '">' .addslashes($value['album_name']). '</option>';
    	}
    	$html .= '</select>';
    
    	return $html;
    }
    
    /**
    * delete an album from the database
    * does not delete the photos in it
    */
    function delete_albums ($album_ids)
    {
    	global $db;
    
    	if (sizeof($album_ids) > 0)
    	{
    		$sql = "DELETE FROM " .ALBUMS_TABLE. "
    			WHERE album_id IN (" .implode(',', $album_ids). ")";
    	
    		if ($db->query($sql))
    		{
    			return TRUE;
    		}
    	}
    
    	return FALSE;
    }
    
    /**
    * delete a set of photos
    */
    function delete_photos ($photo_ids)
    {
    	global $db;
    
    	if (sizeof($photo_ids) > 0)
    	{
    		// select photo names for deletion
    		$sql = "SELECT photo_proper
    			FROM " .PHOTOS_TABLE. "
    			WHERE photo_id IN (" .implode(',', $photo_ids). ")";
    
    		$db->query($sql);
    
    		$unlink_ids = array();
    		while ($row = $db->fetch())
    		{
    			$unlink_ids[] = $row['photo_proper'];
    		}
    
    		// now delete the photos
    		if (sizeof($unlink_ids) > 0)
    		{
    			$sql = "DELETE FROM " .PHOTOS_TABLE. "
    				WHERE photo_id IN (" .implode(',', $photo_ids). ")";
    	
    			if ($db->query($sql))
    			{
    				// remove photo from directory
    				for ($i = 0; $i < sizeof($unlink_ids); $i++)
    				{
    					unlink("photos/" .$unlink_ids[$i]);
    					unlink("photos/thumbs/" .$unlink_ids[$i]);
    				}
    				return TRUE;
    			}
    		}
    	}
    
    	return FALSE;
    }
    
    /**
    * upload a set of files
    */
    function upload_files ($save_dir = '', $album = 0)
    {
    	global $db;
    
    	// check the save dir is writable
    	if (!is_writable($save_dir))
    	{
    		return FALSE;
    	}
    
    	// specify allowed file types
    	$allowed_types = array (
    	
    		'image/jpg',
    		'image/jpeg',
    		'image/pjpeg',
    		'image/gif'
    	
    	);
    
    	// keep a list of uploaded files
    	$uploaded = array();
    
    	// store bytes uploaded
    	$total_bytes = 0;
    
    	// go through the list of files
    	while (list($key, $value) = each($_FILES['userfile']['name']))
    	{
    		// check file has been passed
    		if (!empty($value))
    		{
    			// find extension
    			$extension = substr($_FILES['userfile']['name'][$key], strpos($_FILES['userfile']['name'][$key],".") + 1);
    
    			// time + $key for name
    			$name = time(). "_" .$key. "." .$extension;
    
    			// save url
    			$add = $save_dir. "/" .$name;
    
    			if (move_uploaded_file($_FILES['userfile']['tmp_name'][$key], $add))
    			{
    				chmod($add, 0777);
    
    				// add photo to database
    				create_photo($_FILES['userfile']['name'][$key], $name, $album);
    			}
    		}
    	}
    
    	return TRUE;
    }
    
    /**
    * insert photo details into the database
    */
    function create_photo ($photo_name, $key_name, $album = 0)
    {
    	global $db;
    
    	// get the filesize
    	if (!$size = filesize("photos/" .$key_name))
    	{
    		$size = 0;
    	}
    
    	// insert the photo in the database
    	$sql = "INSERT INTO " .PHOTOS_TABLE. "
    		(
    			photo_name,
    			photo_date,
    			photo_proper,
    			photo_size,
    			album_id
    		)
    		VALUES
    		(
    			'" .addslashes($photo_name). "',
    			" .time(). ",
    			'" .addslashes($key_name). "',
    			" .intval($size). ",
    			" .$album. "
    		)";
    
    	if ($db->query($sql))
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
    
    /**
    * get a complete raw copy of the directory
    */
    function get_dir_contents ($dir)
    {
    	// declare content array
    	$contents = array();
    
    	// check gallery exists
    	if (is_dir($dir))
    	{
    		if ($handle = opendir($dir))
    		{
    			while (FALSE !== ($file = readdir($handle)))
    			{
    				if ($file != '.' && $file != '..' && $file != 'thumbs')
    				{
    					if (file_exists($dir. '/thumbs/' .$file) || thumbnail($file, $dir. '/' .$file, $dir. '/thumbs'))
    					{
    						$contents[] = $dir. '/thumbs/' .$file;
    					}
    					else
    					{
    						$contents[] = 'no_image.gif';
    					}
    				}
    			}
    		}
    	}
    
    	return $contents;
    }
    
    /**
    * create a thumbnail of a photo and store it
    * in the thumbs directory
    */
    function thumbnail ($filename, $source_file, $destination, $new_h = 75, $new_w = 75)
    {
    	if (!is_file($source_file))
    	{
    		return FALSE;
    	}
    
    	// get the image details
    	list($old_w, $old_h, $type) = getimagesize($source_file);
    
    	// create source image
    	switch($type)
    	{
    		case 1:
    
    			$source = @imagecreatefromgif($source_file);
    			break;
    
    		case 2:
    
    			$source = @imagecreatefromjpeg($source_file);
    			break;
    
    		case 3:
    
    			$source = @imagecreatefrompng($source_file);
    			break;
    
    		default :
    
    			return FALSE;
    	}
    
    	// work out dimensions
    	if ($old_w < $old_h)
    	{
    		$thumb_w = $new_w;
    		$thumb_h = $old_h * ($new_h / $old_w);
    	}
    	elseif ($old_w > $old_h)
    	{
    		$thumb_w = $old_w * ($new_w / $old_h);
    		$thumb_h = $new_h;
    	}
    	elseif ($old_w == $old_h)
    	{
    		$thumb_w = $new_w;
    		$thumb_h = $new_h;
    	}
    	$thumb = @imagecreatetruecolor($thumb_w, $thumb_h);
    
    	// create image
    	imagecopyresized($thumb, $source, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_w, $old_h);
    
    	// check we have a proper destination
    	if (!is_dir($destination))
    	{
    		mkdir($destination, 0777);
    	}
    
    	// save image
    	switch ($type)
    	{
    		case 1:
    		case 2:
    
    			imagejpeg($thumb, $destination. '/' .$filename);
    			break;
    
    		case 3:
    
    			imagepng($thumb, $destination. '/' .$filename);
    			break;
    	}
    
    	// memory saving
    	imagedestroy($thumb);
    	imagedestroy($source);
    
    	// thumbnail created successfully
    	return TRUE;
    }
    
    /**
    * make the table to show the directories and photos
    */
    function make_table ($list, $type, $row = 3)
    {
    	// initialize gallery html array
    	$gallery_html = array();
    
    	// check if we have a list > 0
    	if ($size = sizeof($list))
    	{
    		switch ($type)
    		{
    			case "GALLERIES" :
    
    				$gallery_html[] = '<div id="galleries">';
    				break;
    
    			case "PHOTOS" :
    			case "RAW" :
    
    				$gallery_html[] = '<div id="photos">';
    				break;
    		}
    
    		// loop through list
    		for ($i = 0; $i < sizeof($list); $i++)
    		{
    			// shall we open a new list
    			if ($i % $row == 0)
    			{
    				$gallery_html[] = '<ul>';
    			}
    
    			// add to list
    			switch ($type)
    			{
    				case "GALLERIES" :
    
    					// create the delete link
    					$delete_link = '';
    					if ($_SESSION['logged_in'])
    					{
    						$delete_link = '[ <a href="admin.php?mode=deletealbum&a=' .$list[$i]['album_id']. '">Delete</a> ]';
    					}
    
    				
    					$gallery_html[] = '<a href=portfolio.php?albums='.$list[$i]['album_id'].'><img src="folder.gif" class="directory"><br />' .addslashes($list[$i]['album_name']). '</a><br />' .$delete_link. '</li>';
    					break;
    
    				case "PHOTOS" :
    
    					// construct photo name
    					$name = $list[$i]['photo_proper'];
    
    					// construct location
    					$location = "photos/";
    
    					// create thumbnail
    					if (!file_exists($location. "/thumbs/" .$name))
    					{
    						thumbnail($name, $location. "/" .$name, $location. "/thumbs/");
    					}
    
    					// construct date
    					$date = '<div class="date">' .date("d/m G:ia", $list[$i]['photo_date']). '</div>';
    
    					// create the delete link
    					$delete_link = '';
    					if ($_SESSION['logged_in'])
    					{
    						$delete_link = '[ <a href="admin.php?mode=deletephoto&p=' .$list[$i]['photo_id']. '">Delete</a> ]';
    					}
    
    					// image information
    					list($width, $height) = getimagesize($location . $list[$i]['photo_proper']);
    
    					// add to the html
    					$gallery_html[] = '<li><a href="' .$location . $list[$i]['photo_proper']. '" rel="lightbox[racing]" title="Photos"><img src="' .$location. "/thumbs/" .$name. '" class="picture"><br />View</a><br />' .$delete_link. '</li>';
    					break;
    
    				case "RAW" :
    
    					$gallery_html[] = '<li><img src="' .$list[$i]. '" class="picture"></li>';
    					break;
    			}
    
    			// shall we close the list
    			if ( ($i + 1) % $row === 0 || ($i + 1) == $size)
    			{
    				$gallery_html[] = '</ul>';
    			}
    		}
    
    		$gallery_html[] = '</div>';
    	}
    
    	return implode('', $gallery_html);
    }
    ?>
    PHP:

     
    macaela, Sep 2, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    can you please just put the code that do the transferring.
     
    bartolay13, Sep 2, 2010 IP
  3. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    i think this is the line

    function upload_files ($save_dir = '', $album = 0)
    {
        global $db;
    
        // check the save dir is writable
        if (!is_writable($save_dir))
        {
            return FALSE;
        }
    
        // specify allowed file types
        $allowed_types = array (
        
            'image/jpg',
            'image/jpeg',
            'image/pjpeg',
            'image/gif'
        
        );
    PHP:
     
    macaela, Sep 3, 2010 IP