scmeeker Posted August 19, 2010 Share Posted August 19, 2010 I'm having a problem with getting the timestamp to attach to the filename. Currently when it's uploaded in the database it says $timestamp in front of the filename instead of the time. Scroll down towards the bottom of the code to see it. Thanks for your help! $username = $_GET['username']; $item_id = $_GET['id']; define( 'DESIRED_IMAGE_WIDTH', 150 ); define( 'DESIRED_IMAGE_HEIGHT', 150 ); $source_path = $_FILES[ 'thumb' ][ 'tmp_name' ]; $timestamp = time(); // // Add file validation code here // list( $source_width, $source_height, $source_type ) = getimagesize( $source_path ); switch ( $source_type ) { case IMAGETYPE_GIF: $source_gdim = imagecreatefromgif( $source_path ); break; case IMAGETYPE_JPEG: $source_gdim = imagecreatefromjpeg( $source_path ); break; case IMAGETYPE_PNG: $source_gdim = imagecreatefrompng( $source_path ); break; } $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT; if ( $source_aspect_ratio > $desired_aspect_ratio ) { // // Triggered when source image is wider // $temp_height = DESIRED_IMAGE_HEIGHT; $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio ); } else { // // Triggered otherwise (i.e. source image is similar or taller) // $temp_width = DESIRED_IMAGE_WIDTH; $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio ); } // // Resize the image into a temporary GD image // $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height ); imagecopyresampled( $temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height ); // // Copy cropped region from temporary image into the desired GD image // $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2; $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2; $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); imagecopy( $desired_gdim, $temp_gdim, 0, 0, $x0, $y0, DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); // // Render the image // Alternatively, you can save the image in file-system or database // header( 'Content-type: image/jpeg' ); imagejpeg( $desired_gdim, "image_files/".'$timestamp'.$_FILES["thumb"]["name"] ); mysql_connect("localhost", "root", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); $timestamp = time(); $item_image = '"$timestamp"'.$_FILES['thumb']['name']; $sql="UPDATE product SET thumb='$item_image' WHERE id = '$item_id'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/211217-trying-to-add-time-stamp-to-file-name-to-make-it-unique/ Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2010 Share Posted August 19, 2010 Change the quotes around $timestamp from single to double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/211217-trying-to-add-time-stamp-to-file-name-to-make-it-unique/#findComment-1101385 Share on other sites More sharing options...
DavidAM Posted August 19, 2010 Share Posted August 19, 2010 I just glanced through the code quickly. I see two lines that need to be changed: imagejpeg( $desired_gdim, "image_files/".'$timestamp'.$_FILES["thumb"]["name"] ); // AND A FEW LINES BELOW THAT ... $item_image = '"$timestamp"'.$_FILES['thumb']['name']; If you put single quotes around a variable name, it is NOT interpreted as a variable -- you get the variable name NOT the value. If you put double quotes around a variable name, it is interpreted and you get the value. You are NOT REQUIRED to but quotes around variable names -- the double-quote interpretation is for convience in building strings. The two lines I mentioned could be re-written as: imagejpeg( $desired_gdim, "image_files/" . $timestamp . $_FILES["thumb"]["name"] ); // AND A FEW LINES BELOW THAT ... $item_image = $timestamp .$_FILES['thumb']['name']; Also just above that second line, you have changed $timestamp to the current time again. This could be a different value from the one you retrieved at the top of the script, so your filenames may not match up anymore. Take this second assignment out unless you are intentionally trying to change the filename - which is not going to work if the script runs in under 1 second. Quote Link to comment https://forums.phpfreaks.com/topic/211217-trying-to-add-time-stamp-to-file-name-to-make-it-unique/#findComment-1101387 Share on other sites More sharing options...
scmeeker Posted August 19, 2010 Author Share Posted August 19, 2010 Thanks SO much for your help! It works perfect now. It's truly all the little things. Quote Link to comment https://forums.phpfreaks.com/topic/211217-trying-to-add-time-stamp-to-file-name-to-make-it-unique/#findComment-1101389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.