Page 1 of 1

Upload, rename, insert

Posted: Sun Feb 26, 2012 11:05 am
by MiniMonty
Hi all,

tried this a few ways but I'm at the limit of my knowledge.
I'm using this code to upload an image to a server file system (dir), rename it along
the way with a unique id and then to log it's location on an SQL db (along with the headline
and article it illustrates).

All works fine except one nasty detail - the DB record always lists the files original name - not the new timestamped one.
Can anyone see my mistake or suggest a better way ?

Worth knowing that the code will also be used (from the same form) to publish headlines and articles without pictures.

Best wishes
Monty

Code: Select all

	$filename = ( $_FILES['Filedata']['name']);
	$ext = pathinfo($filename, PATHINFO_EXTENSION);
	// strip the file extension
	$file_without_ext = substr($filename, 0, -4);
	// rename file with random (timestamp)
	$randFilename = uniqid('img_');
	$newFilename = $randFilename.".".$ext;
	if ($_FILES['Filedata']['name']) {
		move_uploaded_file($_FILES['Filedata']['tmp_name'], '../news/images/' . $newFilename);
	}
	//  NEDD TO RESIZE THE IMAGE HERE
	
	///////
	
	if (isset ($_POST['headline'])){ 
	$headline = $_POST['headline'];
        $article = $_POST['article'];

	
	// Add  info into the database table "articles"
	include_once "../scripts/connect_to_mysql.php";
        $sql = mysql_query("INSERT INTO articles (headline, article, pic1, date) 
         VALUES('$headline','$article', '$newFilename', now())")  
         or die (mysql_error());	
	}

Re: Upload, rename, insert

Posted: Sun Feb 26, 2012 4:30 pm
by litebearer
What debugging have you done? (ie echo your variables and your query)