Jump to content

How do I import all images beginning with the same characters?


Go to solution Solved by CroNiX,

Recommended Posts

My first post.  :)

 

I am trying to import images related to a particular listing ID.  Currently, I have to enter the number of available photos into a db table, and run the following loop;

 

<section id="photos">

 

<?php

$b = $row['PIX'];


for ( $a = 01;  $a <= $b;  $a++ )

{
echo "<a href=\"../imgs/{ $row[ 'ID' ] }-" . { $a } . ".jpg\" ><img src=\"../imgs/{ $row[ 'ID' ] }-" . { $a } . "_tn.jpg\"  /></a> " ;

}

?>

</section>

 

I would like to find a way to import all the images that begin with the $row[ 'ID' ], but without me having to enter/store { $b } (the number of images recorded in the db).   How can I accomplish this?

 

 

_

Sorry, that's still pretty vague since you used "select *" instead of listing the actual column names. What does $row look like after you perform the query?

 

Do you have a separate table to store the image names? If you did then you could just use a JOIN and not store 'PIX' at all in the listings table. That's the purpose of a relational database.

 

listings:

-id

 

listing_images:

-ID

-listing_id (references listings.id)

-image_name

I do not have a need to keep the name of the file in the db for the following reason,

 

if a row (db entry) has an ID value of  'q123456',  then I have it's associated images named; 

 

q123456-1.jpg,  q123456-1_tn.jpg,

q123456-2.jpg,  q123456-2_tn.jpg,

 

etc,  all in the same directory.  So if this particular listing has 16 images I would manually have to enter that value in the db as  PIX=16

 

I would like to know if I can avoid that step, and somehow import all the images that begin with 'q123456', without the designation of how many images to check for. 

  • Solution

Can probably easily do that with glob()

$ID = 'q123456';
foreach (glob('/path/to/images/' . $ID . '-*') as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

This worked like a charm,  thank you so much!!!

 

One last question, 

 

I use this code to retrieve the thumbnails;
('../imgs/homes/' . $mls . '_*_tn.jpg')

 

ie.  A1736620_01_tn.jpg

 

 

But when trying to retrieve the original image, I get both;
('../imgs/homes/' . $mls . '_*.jpg')

ie.  A1736620_01.jpg

      A1736620_01_tn.jpg

 

How can I use the wildcard char to isolate just the original image?

Not going to pretend to be an expert with glob(), but it wouldn't be easy and maybe not possible due to the way your filenames are, and glob's regex is inclusive (can't exclude results).

 

So, I'd either rename your regular non-thumbnail files to something like:

q123456-1_full.jpg,  q123456-1_tn.jpg

and then you can use:

foreach (glob('/path/to/images/' . $ID . '-*_full.jpg') as $filename) {

or just test each filename when outputting to see if it contains "_tn.jpg" before outputting

$ID = 'q123456';
foreach (glob('/path/to/images/' . $ID . '-*') as $filename)
{
   //make sure it's not a thumbnail
   if (stripos($filename, '_tn.jpg') === FALSE)
   {
      echo "$filename</br>";
   }
}

Another option is to put your "full" files in a separate dir from the thumbnails. Then grab all of the full files, and when you need to output the thumbnails you can add the "_tn" to the filename and just change the path.

Edited by CroNiX
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.