Upload, rename, insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Upload, rename, insert

Post 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());	
	}
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Upload, rename, insert

Post by litebearer »

What debugging have you done? (ie echo your variables and your query)
Post Reply