Jump to content

php image filename detect


thunderstorm

Recommended Posts

Hi everyone.

 

I have a small problem.

I have a database that shows student images, in the format of 1234.jpg but the file name extension is in both upper case and lower case.

My server OS is linux ubuntu. apache2 php5.

Linux see these as 2 different file so will not show the upper case ones. 1234.JPG

 

What i have already is working but for lower case only, i need something to detect when the extension is in uppercase and change accordingly.

 

This is what i have now that works for lower case only.

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());?>.jpg height=150 width=100 border=1px </img

 

This is what i have tried but gives me a filename of "1234jpg" see the "." is missing? and is still lower case.

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $image = jpg;
      $image2 = JPG;
      
      if ($image = $image) {
      echo $image;
      } else {
	      echo $image2;
	      };?> height=150 width=100 border=1px </img>

 

can anyone offer any help please?

 

TIA

Peter

Link to comment
Share on other sites

Let's take a look at your, shall we say, very poorly formatted code.

 

First off, I recommend you look at how you should write HTML so that it is clean and neat. Let's do some re-arranging, but first some explaining.

 

Here is your PHP, I've commented all the things that are wrong with it.

<?php
    // safe to assume that H is a function you have made.
    echo H($mbr->getBarcodeNmbr());

    // if you had error reporting on, php would be telling you about an undefined constant, assumed 'jpg'. make sure that all your strings are surrounded by quotes. also use better names for you variables, such as $lower and $upper
    $image = jpg;
    $image2 = JPG;

    // your using the assignment operator, and the wrong variables. this is what your if statement is saying.
    // if jpg = jpg, which will always be true because you are ASSIGNING $image to $image, not comparing. see the links below.
    // so even if the extension was uppercase it would still show the lowercase
    if ($image = $image) {
        echo $image;
    } else {
        echo $image2;
    }; // i'm not even sure where this semi colon came from, it's not required.
?>

 

Check out assignment operators and comparison operators.

 

Apart from all that, how are you getting the actual images extension?

Link to comment
Share on other sites

Hi ProjectFear

 

Thanks for your reply.

I know my codes are rubbish, i am but a novice and i do try.

 

Here is the re-written code based on what you said. it works for lower case as you said  it would. i am having a read on those suggestions you made and will see if i can work it out, if not i will ask here again.

 

The extension comes from a folder of images.

So i guess the script will have to read the file first to determine what case the extension is, correct?

 

I thought the formatting was correct for displaying an image? if i move the php tag and such, it breaks

The whole code is inbetween the img tags

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $lower = ".jpg";
      $upper = ".JPG";
      
      if ($lower = $lower) {
      echo $lower;
      } else {
	      echo $upper;
	      }
	      ?> height=150 width=100 border=1px </img>

 

Thanks

peter

Link to comment
Share on other sites

You could place this code above the image tag, then set a variable to use in the image tag.

 

Your if statement still makes no sense. What is stored in the database, the whole filename including the extension or just the filename? Why can't you store the extension in the database, thus removing the need for this?

Link to comment
Share on other sites

Your if statement still makes no sense.

 

yes i know it still does not make sense, i was just showing you the changes so far and then i was going to go have a read from the site you posted.

 

Why can't you store the extension in the database, thus removing the need for this?

 

more code i need to figure out?

 

the image prefix name comes from the barcode number in the database (student id) ex: 1234

their photo is formatted 1234.jpg OR 1234.JPG in a file in a folder.

the photos come from the student management system which is a non sql windows system. i copied them to the linux server to a folder.

 

This thing i am working on is the library system and i thought it would be good for the librarian to see a photo of the student loaning books so she can see if its the right kid or not.

 

Peter

 

 

Link to comment
Share on other sites

HI

 

still stuck on this if anyone has some code to achieve this.

 

Here's what i have, and yes it sucks but at least i am trying.

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $lower = ".jpg";
      $upper = ".JPG";
      
      if ($lower != $lower) {
      echo $lower;
      } else {
      echo $upper;
      }
      ?> height=150 width=100 border=1px </img>

 

Thanks

peter

Link to comment
Share on other sites

Replace this:

 

if ($lower = $lower) {
      echo $lower;
      } else {
      echo $upper;
}

 

With:

if (file_exists('/path/to/image/dir/' . H($mbr->getBarcodeNmbr()) . '.' . $lower)) {
      echo $lower;
   } else {
      echo $upper;
}

Place in the correct path the where the image is stored.

Link to comment
Share on other sites

Hi

thanks for your reply.

 

That has swapped what i had before, it now displays all upper case extension images and ignores lower case ones.

Any further suggestions?

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $lower = ".jpg";
      $upper = ".JPG";
      
      if (file_exists('../images/mbr_images/' . H($mbr->getBarcodeNmbr()) . '.' . $lower)) {
      echo $lower;
   } else {
      echo $upper;
}
	      ?> height=150 width=100</img>

 

Code as it is now with your additions.

 

Thanks

Peter

Link to comment
Share on other sites

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $lower = ".jpg";
      $upper = ".JPG";
      
      if (file_exists('../images/mbr_images/' . H($mbr->getBarcodeNmbr()) . '.' . $lower)) {
      echo $lower;
   } else {
      echo $upper;
}
	      ?> height=150 width=100</img>

 

 

I believe the problem is, This would echo eg, 1234..jpg as there is a period in the File_exists and in the $lower variable.

 

This should work

 

<img src=../images/mbr_images/<?php echo H($mbr->getBarcodeNmbr());
      
      $lower = "jpg";
      $upper = ".JPG";
      
      if (file_exists('../images/mbr_images/' . H($mbr->getBarcodeNmbr()) . '.' . $lower)) {
      echo $lower;
   } else {
      echo $upper;
}
	      ?> height=150 width=100</img>

     

Link to comment
Share on other sites

Hi there

MasterK and ProjectFear

 

Thank you 2 for helping with code.

It now works, i posted the code below for others.

 

Based on your idea MasterK, i removed a period from inbetween '.' and it worked.

It now displays all the student images and the register clerk can carry on doing what she does with disregard to file names.

 

Thank you

Peter

 

$lower = ".jpg";
      $upper = ".JPG";
      
      if (file_exists('../images/mbr_images/' . H($mbr->getBarcodeNmbr()) . '' . $lower)) {
      echo $lower;
   } else {
      echo $upper;
}

 

PS for others using the code please replace the

H($mbr->getBarcodeNmbr())

with however you get your images from the database. In my case it was barcode numbers that matched image name.

 

Thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.