user1174762
user1174762

Reputation: 593

Storing an image with PHP

I want to let users upload Images. The file gets re-named (by adding the time() to the current name) and then moved to my Images/ folder. I then reference the name uploaded into the database table in my html/php code. The images won't appear because the name in the table is different from the actual file name. The image name needs to be changed in-case two users ever upload a file with the same name. How can I apply the time() name change to both the file name in the table and the actual file name so the images appear. Here is my code:

if (!empty($_FILES['picture'])) 
    {
    $pic= $_FILES['picture']['name'];
    $target= GW_UPLOADPATH . time() . $pic;
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $target))
            {
            $query= "INSERT INTO posts (user_id, story, picture) VALUES     
('$user_id', '$story', '$pic')";
            mysqli_query($connect, $query);
            header( 'Location: profile.php' );
            }
    }

The picture name in the table is just ball.jpg and the actual file might be 18292018ball.jpg.

Upvotes: 0

Views: 162

Answers (9)

Rohit
Rohit

Reputation: 1

Use this :

$query= "INSERT INTO posts (user_id, story, picture) VALUES ('$user_id', '$story', '$target')";

Upvotes: 0

heyanshukla
heyanshukla

Reputation: 669

Try this

$query= "INSERT INTO posts (user_id, story, picture)
         VALUES ('$user_id', '$story', '$target')";

Upvotes: 1

xkeshav
xkeshav

Reputation: 54060

note : if you are adding time() as a prefix then it will be never the same for two images

and when you changed your file name then why dont you store that new name in db

$changed_name = time().$pic;

$query= "INSERT INTO posts (user_id, story, picture) VALUES     
($user_id, '".$story."', '".$changed_name ."')";

TIPS to change file name

  • first take out the extension and name
  • remove multiple spaces by single space
  • then replace space with _
  • convert the case of image name ( I prefer lowercase )
  • then add the time() with image name
  • finally add that extension and store into image folder

Upvotes: 1

Dhruvisha
Dhruvisha

Reputation: 2530

Try this:

if (!empty($_FILES['picture'])) 
{
$pic= $_FILES['picture']['name'];
$picname = time() . $pic;
$target= GW_UPLOADPATH . $picname;
    if (move_uploaded_file($_FILES['picture']['tmp_name'], $target))
        {
        $query= "INSERT INTO posts (user_id, story, picture) VALUES('$user_id', '$story', '$picname')";
        mysqli_query($connect, $query);
        header( 'Location: profile.php' );
        }
}

$picname will contain time+picture name. and it will be same if more than one user uploads at the same time.

Upvotes: 0

Krishna Deepak
Krishna Deepak

Reputation: 1765

No need to store the upload path everytime, unless it might be different at different points in time.

$name = time() . $pic;
$target= GW_UPLOADPATH .$name;

$query= "INSERT INTO posts (user_id, story, picture) VALUES
('$user_id', '$story', '$name')";

Upvotes: 0

Octavian Vladu
Octavian Vladu

Reputation: 1250

If the path is the same for all (ex. Images/ folder) you can use this:

if (!empty($_FILES['picture'])) {
    $pic= time() . $_FILES['picture']['name'];
    $target= GW_UPLOADPATH . $pic;

        if (move_uploaded_file($_FILES['picture']['tmp_name'], $target)) {
            $query= "INSERT INTO posts (user_id, story, picture) VALUES ('$user_id', '$story', '$pic')";
            mysqli_query($connect, $query);
            header( 'Location: profile.php' );
        }
}

Upvotes: 0

ahruss
ahruss

Reputation: 2130

You're storing in the database a different filename from the one you're saving. If you want to use just the filename, store the filename in a variable before appending it to the path, i.e, instead of

$target= GW_UPLOADPATH . time() . $pic;

do

$fileName = time() . $pic;
$target = GW_UPLOADPATH . $fileName;

and use $fileName in place of $pic in your SQL query:

    $query= "INSERT INTO posts (user_id, story, picture) VALUES ('$user_id', '$story', '$fileName')";

Upvotes: 0

Frankie
Frankie

Reputation: 25165

To be exactly sure there aren't any dupes:

  1. server gets image (user uploads image somehow)
  2. server generates a unique ID for the image
  3. server makes a write on the database (on a unique field) with that id
    • on success move the file to the proper place on disk
    • on failure restart on step two generating a different ID

Upvotes: 0

asprin
asprin

Reputation: 9833

Shouldn't you use '$target' instead of '$pic' in you MySql query?

$query= "INSERT INTO posts (user_id, story, picture) VALUES ('$user_id', '$story', '$target')";

Upvotes: 1

Related Questions