Hi all,

Over the last week or two i have been working on uploading images to mysql db,

Whilst i have the image uploading to the correct location and the name of the image added to the database, i cant understand how to add the image location to the name also,

I have the following code that uploads & names the image as of the date time of upload, but I would like to add the location also to the database so I can call it back later.

// Image upload process start
			$image_ext = explode('.', $image);  //split the image name with respect of " . "
			$image_name = date('Ymdhis') . "." . $image_ext[1];  //create unique image name with present "date & time"
			
			$dest = "uploaded/" . $image_name; //set the destination path (where image file will be uploaded)
			$val = move_uploaded_file($_FILES['profile_image']['tmp_name'], $dest); //upload image to the destination path
			// Image upload process end

many thanks

Recommended Answers

All 12 Replies

Surely you would want to use the value of $dest to store the file location in the DB..

you want to add the location on the database? then just insert your $dest variable into your database.

assuming you had these table and variables
you query should look like this

INSERT INTO your_table(name,date,location) VALUES('$name','$date','$dest');

hi, I have tried both

$image_name = date('$dest' 'Ymdhis') . "." . $image_ext[1];

//and i have tried this 

$image_name = date('uploaded/' 'Ymdhis') . "." . $image_ext[1];

but i get an error all the time ??

what id like it to read in the database is
uploaded/imagename.jpg

hi, I have tried both

$image_name = date('$dest' 'Ymdhis') . "." . $image_ext[1];

//and i have tried this 

$image_name = date('uploaded/' 'Ymdhis') . "." . $image_ext[1];

but i get an error all the time ??

Perhaps you should take another look at the Date function..

@Will

Will how do you mean, ?? Look at the date function ???

What could be the problem with that date function

The error you were getting, is because wrong date() use... So far you tried to merge the strings inside the function, while date() gets only correct date modifiers.

Try that...

$image_name = date('Ymdhis') . "." . $image_ext[1];
$image_name = $dest.$image_name;

BTW doing explode() to get image extension could be wrong, because some images can be named, for example, like "image.test.jpg".

cheers Will for the pointers,

I have tried what you suggested and im still getting the image name but no location ??

If you echo $dest what does it say?

will its now working correctly with the following code - cheers for your help bud, I will mark this as solved

$image_ext = explode('.', $image);  //split the image name with respect of " . "
			$image_name = date('Ymdhis'). "." . $image_ext[1];  //create unique image name with present "date & time"
			//$image_name = $dest.$image_name;
			$dest = "uploaded/" . $image_name; //set the destination path (where image file will be uploaded)
			$image_name = $dest;
			$val = move_uploaded_file($_FILES['profile_image']['tmp_name'], $dest); //upload image to the destination path
			// Image upload process end

Good :)

You will need to alter this bit:

$image_ext = explode('.', $image); 
$image_name = date('Ymdhis'). "." . $image_ext[1];

If the user uploads an image with the name 'picture.123.456.jpg' for example, that script will not work.

Do something along the lines of this:

$image_ext = explode('.', $image); 
$actual_ext = count($image_ext) -1; // Get the string after the final period.
$image_name = date('Ymdhis'). "." . $image_ext[$actual_ext];

Hi will,

I have tried what you suggested !

I renamed one image to image.name.jpg and tried to register, and it failed the script as you said it would,

I added the code you suggested but I was still getting an error saying the file should be jpg,png etc etc

I have edited my error flag to say, images should be imagename.jpg not
image.name.jpg

regards
Lloyd

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.