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

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.