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.

Dani AI

Generated

Quick summary and immediate cause: correctly diagnosed the original bug (variable interpolation), which explains why the folder and file now exist on disk. The remaining display problem almost always comes from what was stored in the database (filesystem path or HTML) versus what the browser needs (a web-accessible URL). The database should hold a clean web path or filename — not the server's full filesystem path and not an HTML anchor.

Convert a filesystem path to a web URL before output. For example, when the DB contains a full server path (like /home/you/public_html/members/...), remove the document-root prefix and output a root-relative URL:

$fs = $row['img1'];                  // filesystem path from DB
$web = '/' . ltrim(str_replace($_SERVER['DOCUMENT_ROOT'], '', $fs), '/');
echo '<img src="' . htmlspecialchars($web, ENT_QUOTES) . '" alt="profile">';

Best-practice reminders (expand on points from ): store only a relative URL or filename in the DB (e.g. /members/uploaded/12/pic.jpg); keep the file extension (use pathinfo() when renaming); validate uploads with getimagesize() or finfo to ensure true image types; set sensible permissions (directories 0755, files 0644); and never store raw HTML in the DB — store data, not markup.

Troubleshooting checklist: open the image URL directly in a browser to confirm a 200 response; inspect the rendered <img> src with browser devtools; verify move_uploaded_file() returned true and file_exists() on disk; check server error logs and any .htaccess rules blocking access; confirm case-sensitive filename matches on Linux. Storing files under the web root is simplest; if files are stored outside web root for security, serve them via a small PHP script that validates access and streams the file.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

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.

-

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.