I have an image upload script that takes a large picture and resizes it to a web-friendly size, then saves it to the ./../pgallery/ folder. The script then creates a thumbnail image of this resized picture and saves it to the ./../pgallery/thumbs/ folder. The image is ultimately saved as $imagepath in the folders. I’m not sure what part the $source is playing.
The pictures are placed in a folder on the server, and the image name and sizes of the big picture and thumbnail are put into a database table.
The script works well, but I need to make the images unique to the user. I’d like to be able to assign the user’s user_id to the image. The user_id is in a session variable:
[code=php]$_SESSION[‘user_id’]
How can I do this? I’ve tried several things but nothing has worked.
I need the images in the folders to match the name that’s put into the database. Ideally, it would look like this – 12_car.jpg where 12 is the user’s user_id. user_id + an underscore + image.jpg.
The script starts out by taking the image name from the form and if it isn’t empty, converts it to all lower characters and checks the database to see if that name is already in use. If that image name is already in the database an error message is returned telling them to either change the image name or select another picture.
If the image isn’t already in the database it gets processed.
[code=php]$new_image = $_FILES[‘new_image’][‘name’];
if ($new_image != ”)
{
$new_image = strtolower($new_image);
// Make sure the image image_name isn’t already in the database.
$query = “SELECT id FROM ads WHERE image_name=’$new_image'”;
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
if(isset($_POST[‘submit’])){
if (isset ($_FILES[‘new_image’])){
$imagename = $_FILES[‘new_image’][‘name’];
$imagename = strtolower(“$imagename”);
$source = $_FILES[‘new_image’][‘tmp_name’];
$target = “./../pgallery/”.$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = “./../pgallery/” . $imagepath; //This is the new file you saving
$file = “./../pgallery/” . $imagepath; //This is the original file
Any help would be appreciated.
Thanks
Alan P