Jump to content

Displaying images with different extensions


Scorptique

Recommended Posts

Hey all, I need some help ^^;;

 

I want to store my images in the database then display it in a gallery.

The image schema is: Image(id, name)

 

The way I thought of is to connect to database and extract the image name, (eg. rose). Then I will echo it out

 

echo "<img src=images/$name.jpg></img><br>";

 

But what happens if my images have different extensions? like .gif, .png, .bmp. How am I suppose to display my images?

How do I extract the extension?

 

I can't manually store the extension names because it's not me who is uploading the pictures. I am doing a web application which allows users to upload their images and I dont expect them to type in the image extension in a different field everytime they want to upload sth.

 

Help guys, thanks! ^^;;

you can store the extension when a user uploads a file in php in the $_FILES array there is a field called name.

if you go

$ext = substr($_FILES['uploadedfile']['name']

$ext = substr($_FILES['uploadedfile']['name'],strrpos($_FILES['uploadedfile']['name'],".")+1);

it will output the extension.

you can save that in the database and pull it out when displaying the image.

 

Scott.

Thanks scott, I sort of understand what you're trying to say.

But does this only work when the user uploads a file?

What about my other images I added into the folder initially to get my website going?

Do I have to manually key in the extensions?

 

Thanks.

are all these images in the same folder?

you could do something like this

$files = glob("*.*");
$valid_exts = array("jpg","jpeg","gif","png","tiff");
foreach($files as $file){
        $ext = substr($file,strrpos($file,".")+1);
        if(in_array($ext,$valid_exts)){
                echo "$file is a image<br>";
                $name = substr($file,0,strrpos($file,"."));
                echo "File is called {$name}<br>";
                $query = "UPDATE `images` SET `ext` = '{$ext}' WHERE `name` = '{$name}'";
                mysql_query($query);
        }
}

it should update all your existing images.

 

Scott.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.