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
https://forums.phpfreaks.com/topic/228629-random-file-name/
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
https://forums.phpfreaks.com/topic/228629-random-file-name/#findComment-1178822
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.