1

I have an images table in database which holds ID(it's an auto increment post number), filename(uploaded files own name) and timestamp etc..

I upload files to a directory and add the info to the table. I'd like to change the filename with the ID number(or order number) from database table in the directory after I upload and put info in the database table.

keep in mind that I'm somewhat beginner at php and I'd like to learn with the explanation of the code. like which string is for what, which method is for what..

thank you very much for your time.

here's some code:

    //CONNECTS TO DATABASE
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if (!$link) 
    {
        die('Could not connect: '. mysqli_error());
    }

    //SELECTS DATABASE
    $db_selected = mysqli_select_db($link, DB_NAME);
    if (!$db_selected) 
    {
        die('Can\'t use' . DB_NAME . ': ' . mysqli_error());
    }

    //FILE NAME
    $userNameSend = $_FILES ['user-file']['name'];

    //INSERTS INTO DATABASE
    $sql = "INSERT INTO images (USERIMAGENAME) VALUES ('$userNameSend')";

    //CONNECTION CHECK
    if (!mysqli_query($link, $sql)) 
    {
        die('Error: ' . mysqli_error());
    }
    else 
    {
        header('Location: ./board.php');
    }
}
mysqli_close($link);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
idexo
  • 63
  • 10

1 Answers1

1

After the insert (if successful) get the id of the last inserted row:

$last_id = mysqli_insert_id($link);

Get the extension of the uploaded file:

$ext = pathinfo($userNameSend, PATHINFO_EXTENSION);

Then rename the uploaded file with ($upload_dir is the path to the directory where you are saving the files):

rename($upload_dir.$userNameSend, $upload_dir.$last_id.".".$ext);
MazzCris
  • 1,812
  • 1
  • 14
  • 20
  • hi, thank you. I'm trying your code. it changes the name, but I only get the name "0" why do you think it only changes to "0.jpg"? is it a connection error or it can't get the ID when connected? – idexo Aug 12 '15 at 08:40
  • Does the insert succeed? Is the assignment $last_id = mysqli_insert_id($link) after the insert? This answer may be useful: http://stackoverflow.com/a/8243685/3052648 – MazzCris Aug 12 '15 at 08:43
  • I've come across this answer: http://stackoverflow.com/a/13882839/5217950 I'll try to check this out first. I appreciate your help. – idexo Aug 12 '15 at 08:48
  • I saw persistent connections was FALSE, I changed it to true, still nothing, then I tried $last-id = mysql_insert_id($this->db); still nothing. there is an auto increment column in the table, table works fine. I assume it can't get connected. – idexo Aug 12 '15 at 09:01
  • I finally got it to work. somehow (I put it in the right place.) this was my mistake obviously. first I didn't get the directory right. > then it started to give me "0" I changed the phpmyadmin config file persistent connections to TRUE > then I placed it in the if statement with the right upload directory settings and now it works quite good. – idexo Aug 12 '15 at 10:19