i am trying to store image in folder and 2nd part is store image name in database.
this works fine but the problem is that no matter how many images u upload, there is only one image in folder name 0.jpg.

 $sql = mysql_query("SELECT * FROM item WHERE image_user_name = '$image_user_name_p'");
$existCount = mysql_num_rows($sql);
if($existCount >= 1)
    {
        $add_item_error .= "That image name is already taken";
         exit();
    }

   //place image in folder
        $pid = mysql_insert_id(); 
        $image_folder_name = "$pid.jpg";
        move_uploaded_file($_FILES['image_file']['tmp_name'], "IMAGE/ITEMS/$image_folder_name"); 

    //add this product into the database now
    $sql = mysql_query("INSERT INTO item (image_folder_name)
    VALUES('$image_folder_name'");

i am trying to set $pid be different every time. ex 0,1,2,3,4,5....
and iam getting the id of image from database by mysql_insert_id();
so if in database image last image id is 3. than $pid = 3. this way i will get different value for pid

Member Avatar for LastMitch

This function mysql_insert_id()

Read this:

http://php.net/manual/en/function.mysql-insert-id.php

Try another alternative.

You forgot to put another parentheses here:

From this:

$sql = mysql_query("INSERT INTO item (image_folder_name) VALUES('$image_folder_name'");

to this:

$sql = mysql_query("INSERT INTO item (image_folder_name) VALUES('$image_folder_name')");
commented: To Rectify what some retard did to LastMitch +0

I would suggest that you create a random filename for your image and keep an attribute "id" in your database table with auto-increment feature. In this way you can get what you want. Use rand(x, y) method.

Or else, according to your procedure, after using mysql_insert_id() - you are getting the last inserted row's id. Right?
So, you can use this value + 1 to set a new id.

Member Avatar for diafol

Alternatively, use a timestamp for the filename:

$filename = time() . '_' . $user_id . '.' . $ext;

That should make a unique filename without chance of collision.

that seem to be a good plan. just one question what is ext?

that seem to be a good plan. just one question what is ext?

$ext is referring to the file extension (.jpeg, .png, etc.).

i tried using: exif function to get the type
$ext = exif_imagetype($_FILES['image_file']['tmp_name']);
but this return '2'.

Member Avatar for diafol

No use pathinfo:

$ext = pathinfo($_FILES['image_file']['name'], PATHINFO_EXTENSION);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.