Hi all,

can someone help me here -
I ave a regisration script where I am uploading an image to mysql,

The problem im having at the moment is this.....

The script creates a new folder by using the mysql_last_id function as shown below -

The problem im having is concatinating the image path to be the document root.

ie. websitename.com/members/uploaded/12/image.jpg

i have tried using $_server document root again as below shows, but the directory is not being created with server document root.

document root option - directory being created as $lastid not id number of 12

$lastid = mysql_insert_id ();

        //Check whether the query was successful or not
        if($lastid > 0)
        {
            $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 = ($_SERVER['DOCUMENT_ROOT'].'/members/uploaded/$lastid');
            mkdir ($_SERVER['DOCUMENT_ROOT'].'/members/uploaded/$lastid', 0755);
            $dest = $dest."/".$image_name;
            $val = move_uploaded_file($_FILES['profile_image']['tmp_name'], $dest); //upload image 

to the destination path
            if($val){
                mysql_query("update memberst set img1='$dest' where id=$lastid");
            }

concatination option - no image being uploaded

if($lastid > 0)
        {
            $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/$lastid";
            mkdir("uploaded/$lastid", 0755);
            $dest = $dest."/".$image_name;
            $val = move_uploaded_file($_FILES['profile_image']['tmp_name'], $dest); //upload image to the destination path
            if($val){
                mysql_query("update memberst set img1='http://www.websitename.com/members/$dest where id=$lastid");
            }

im not sure what is the best option to get both directory created and image path uploaded to the database.

hope someone can point me in the right direction.

Member Avatar for diafol

Can I ask why you're creating a new folder for each image? It seems a bit over the top to me. Also it seems that the image is being saved without a file extension.

For example if you upload image123.png, it gets stored to:

http://www.websitename.com/members/uploaded/1328/20100528100957.image123

The db record shows: id = 1328, img1 = http://www.websitename.com/members/uploaded/1328/20100528100957.image123

THis looks like a really complicated was of storing images.


However, this should work:

if($lastid > 0)
        {
            $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/$lastid";
            mkdir("uploaded/$lastid", 0755);
            $dest = $dest."/".$image_name;
            $val = move_uploaded_file($_FILES['profile_image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . "/$dest"); //upload image to the destination path
            if($val){
                mysql_query("update memberst set img1='http://www.websitename.com/members/$dest where id=$lastid");
            }

Are you allowing different file formats, like png, gif, jpg, svg? If so, how will your site know which type to serve when an image needs to be shown?

The problem is pretty simple; this has to do with the quotes you are using. When you use single quotes, anything inside it is interpreted as LITERAL. In other words, if you wrote:

$num = 3;

echo 'I have $num apples.';

you would get:

I have $num apples.

Only use single quotes when what it contains should be LITERAL; if you have variables within the string, use double quotes like this:

$num = 3;

echo "I have $num apples.";

This would give you what you expect:

I have 3 apples.

Here is your problem:

$_SERVER.'/members/uploaded/$lastid'

Replace the single quotes with double quotes and the $lastid should be replaced with its value instead.

@Sikosoft -

Many many thanks for showing me the understanding and how you worded is was great thanks, Your solutions work, where as the complete path of the document root was uploaded to the database with the correct (new) ID number of the user registering.

This also created the folder in the correct location and placed the users image in thier folder.

The only problem im having now is displaying the image ??

Any pointers would be great thanks.

If I find the reason behind the image issue, I will post and mark this thread as solved.

regards

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.