Jump to content

Random File Name


sblake161189

Recommended Posts

Hi Guys,

 

I have a php script to upload a photo to a mysql table. This works great, until someone uploads a file with the same name, then it replaces the old one. Bad times!

 

So Ive researched it and I have put a timestamp on the filename and it uploads with the correct timestamp in the images folder.

 

However the data that it uploads to the table is just the original filename, ie it doesnt stamp the filename in the table therefore they dont match...

 

Any ideas...

 

<?php 

include('config.php'); 
if (isset($_GET['Ter']) ) { 
$ter = (int) $_GET['Ter']; 
if (isset($_POST['submitted'])) {

//Photo Upload	
//This is the directory where images will be saved
$name=time();
$target = "images/"; 
$target = $target .$name. basename( $_FILES['photo']['name']);
//This gets all the other information from the form 
$photo = ($_FILES['photo']['name']);
//Pause Photo Upload


foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
$sql= "UPDATE `ter` SET `Ter` =  '{$_POST['Ter']}' , `BranchName` = '{$_POST['BranchName']}' ,  `BranchAddress` =  '{$_POST['BranchAddress']}' ,  `BranchTel` =  '{$_POST['BranchTel']}' , `BranchEmail` =  '{$_POST['BranchEmail']}' , `BranchLink` =  '{$_POST['BranchLink']}' , `Theme` =  '{$_POST['Theme']}' , `LocalInfo` =  '{$_POST['LocalInfo']}' , `BranchInfo` =  '{$_POST['BranchInfo']}' , `photo` =  '{$_FILES['photo']['name']}' WHERE `Ter` = '$ter' "; 

mysql_query($sql) or die(mysql_error()); 


//Unpause Photo Upload
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "<br />The file ". basename( $_FILES['photo']['name']). " has been uploaded. <br />";
}
else {
//Gives and error if its not
echo "";
} //End of Photo Upload

echo (mysql_affected_rows()) ? "<br />Edited Branch.<br />" : "<br />Nothing changed. <br />";  
} 
$row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `Ter` = '$ter' "));
?>

Link to comment
Share on other sites

Perhaps...

 

$name=time();
$target = "images/"; 
$target = $target .$name. basename( $_FILES['photo']['name']);//This gets all the other information from the form 
$photo = $name . ($_FILES['photo']['name']);//Pause Photo Upload

 

IF you want to include the path in the table..

$name=time();
$target = "images/"; 
$target = $target .$name. basename( $_FILES['photo']['name']);//This gets all the other information from the form 
$photo = $target;//Pause Photo Upload

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.