Reputation: 593
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
Reputation: 1
Use this :
$query= "INSERT INTO posts (user_id, story, picture) VALUES ('$user_id', '$story', '$target')";
Upvotes: 0
Reputation: 669
Try this
$query= "INSERT INTO posts (user_id, story, picture)
VALUES ('$user_id', '$story', '$target')";
Upvotes: 1
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 ."')";
_
Upvotes: 1
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
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
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
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
Reputation: 25165
To be exactly sure there aren't any dupes:
Upvotes: 0
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