Jump to content

[SOLVED] Unique Id issue


mcmuney

Recommended Posts

For a file upload site, what is happening is when a file is uploaded, it's being renamed to the unique id. For example, if you upload file xyz.jpg and this is the very first upload, it will create record id 1 in the DB and will rename the file as 1.jpg.

 

Now, lets say that in my DB, I have 5 records and 5 files. Now, lets say that I delete record #5. For the next upload, you'd expect a 5th record in the DB, but the unique id will be 6 (since unique id 5 will not be used again). Here's the problem, it creates the 5th record with unique id 6, but the filename gets names 5.jpg. This creates a problem. Below is the code that's doing this step. Please help.

 

$pt=0;
$sqlPtFila = mysql_query("SELECT * FROM images ORDER BY ID DESC LIMIT 1");
$rowPtFila = mysql_fetch_array($sqlPtFila);
$pt = $rowPtFila[id];
$pt = $pt + 1;
$pt .= ".jpg"; 

if (isset($_POST['tags'])) {

$allowed_types = array( 

     'image/pjpeg', 

     'image/gif', 

     'image/png', 

     'image/jpeg'); 

     if(in_array($_FILES['filename']['type'], $allowed_types)) 

     {

$date = date("m-d-y");
$sql = mysql_query("SELECT * FROM images WHERE date = '$date'");
$row = mysql_fetch_row($sql);
if ($row == NULL) {
mkdir("photos/$date", 0777);
}
$target = "photos/$date/";
$target = $target . basename( $_FILES['filename']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target))
{
echo "<div style='padding-left: 30px; padding-top: 15px; font-family:Tahoma; font-size:11px; color:#575757; font-weight: bold;'>URL: <a href='http://www.pickaloo.com/photos/".$date ."/" . $pt."' style=\"font-family:Tahoma; font-size:11px; color:#FF0000; font-weight: bold;\" target='_blank'>http://www.pickaloo.com/photos/".$date ."/" .$pt. "</a></div>";
}
else {

}
$filename = basename( $_FILES['filename']['name']);

$ip = $_SERVER['REMOTE_ADDR'];
if ($_POST['private']) {
$private = "yes";
}
else {
$private = "no";
}
rename("photos/$date/$filename", "photos/$date/$pt");
$tags=$_POST['tags'];
$sql = mysql_query("INSERT INTO images values('','$filename','$date','$ip','$private','$tags')");

}

Link to comment
https://forums.phpfreaks.com/topic/86351-solved-unique-id-issue/
Share on other sites

Thanks for the suggestion, but the script is already doing that. If row 5 is removed, the DB knows that the next row inserted will be 6, even though only 4 records exist. But when the file is renamed, it looks for the greatest unique ID and adds 1. In this case, the greatest existing id will be 4, then +1 = 5. So the file will be 5.jpg and the DB record 6. Somehow it needs to know what the next unique id the DB will assign.

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.