Upload images

Hello, there is a form for uploading an image, everything works, but I wanted each photo to have a name, as well as an ID in the database, so that there were no matches in the names. Let’s say I upload a photo eqwb312beqw.jpg and it is immediately renamed to 315.jpg.

Answer 1, authority 100%


The function returns the last assigned ID on an INSERT query. What do you need?


The function must be called immediately after the insert. If you need to separately store the name of the image, you should make a subquery when requesting:

function filename_extension($filename) {
 $pos = strrpos($filename, '.');
 if($pos===false) {
     return false;
 } else {
     return substr($filename, $pos);
$extension = filename_extension($picname);
$res = mysql_query ("INSERT INTO images (name,checked,userID,preview) VALUES (concat((SELECT (LAST_INSERT_ID()+1)),'$extension'),'1','$row1[id]',concat((SELECT (LAST_INSERT_ID()+1)),'$extension'))");

Answer 2

remember the number in the file/base. increase by 1 on each new upload and don’t forget to update in the file/database.

Answer 3

Insert a line with all the data into the database (the path to the picture, another info, etc.). You pull Id of the last INSERT. and copy the picture to the place indicated in the table with the name ID.jpg – for example.

Answer 4

I generally prefer to name images a la – microtime().'.'.$type;

There will be no repetitions 100%

$imageinfo = getimagesize($_FILES['file']['tmp_name']);
$type = strtr($imageinfo['mime'], array(
"image/gif" => "gif",
"image/jpeg" => "jpg",
"image/png" => "png",
$uploadfile = microtime().'.'.$type;
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$uploadfile);

Answer 5

In theory, you need to pull out the ID by name

$result = mysql_query("SELECT user FROM * WHERE $user");

Pull the ID out of the request and that’s it.