Jump to content

mod_rewrite and foreach image in directory


JesseToxik

Recommended Posts

Hello all,

 

I have a PHP code that checks a specified directory for image files.

<?php
$files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
  echo ('<img src="'.$file.'">');
}
?>

It is loaded into a page at the following url: 

http://cronanpilotcar.byethost33.com/page/photos

I use mod_rewrite to make a "pretty" url out of ?page=photos.

It simply includes my gallery.php page into my template.

 

When it gets an image, it makes the image url like so: 
http://cronanpilotcar.byethost33.com/page/images/gallery/Mustang.jpg

 

How can I remove the /page/ from the image path?
I plan on having many images in the directory and would love for them to automatically display on upload.

 

Thanks.

 

Link to comment
Share on other sites

Ah, I didn't notice:

$files = glob('/images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
There are two types of file paths: one for the filesystem and hard drive(s), one for URLs. They are often the same simply because that's easier, but technically speaking they are two different things.

 

The URL for the images should have leading slashes. The filesystem path for the images should not. In fact if you do then PHP is going to think you mean to find a "/images" folder at the very root of the drive. But that's not where it is.

<?php
$files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE); // filesystem path with ../
foreach($files as $file) {
  echo ('<img src="/images/gallery/' . basename($file) . '">'); // URL path with /
}
?>
Edited by requinix
Link to comment
Share on other sites

I tried that bit of code to no avail.

I added a line to the code to see if it would echo the word test and it didnt work.

<?php
$files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE); // filesystem path with ../
foreach($files as $file) {
  echo ('<img src="/images/gallery/' . basename($file) . '">'); // URL path with /
  echo('TEST');
}
?>

I've used this code before on a locally hosted site but for  some reason it wont work now.

Link to comment
Share on other sites

Ok so I wrote a new code that works except for one issue. It displays two broken images and the actual image in the folder. 

There is a single image in the directory so I do not understand why it is displaying the two broken images.

<?php
$files = scandir('images/gallery/'); // filesystem path with ../
foreach($files as $file) {
  echo ('<img src="/images/gallery/' . basename($file) . '">'); // URL path with /
}
?>
Link to comment
Share on other sites

I have a new issue:

I found a solution for the desktop version of my site but not the mobile site.

 

<?php
$files = glob('images/gallery/*.{jpg,png,gif}', GLOB_BRACE); // filesystem path with ../
foreach($files as $file) {
  echo ('<img src="/images/gallery/' . basename($file) . '" width="200px">'); // URL path with /
}
?>

The above works on dekstop but not on mobile and the below works on mobile but not on desktop. My mobile url is http://cronanpilotcar.byethost33.com/mobile

 

<?php
$files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE); // filesystem path with ../
foreach($files as $file) {
  echo ('<img src="/images/gallery/' . basename($file) . '" width="200px">'); // URL path with /
}
?>
Link to comment
Share on other sites

Alright, let's just do away with the relative directories entirely as they seem to be the problem over and over again. I should have said that at the start.

 

Unless you're doing something weird with directories, the /images folder is directly under the DOCUMENT_ROOT.

files = glob($_SERVER['DOCUMENT_ROOT'] . '/images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
Using an absolute path like that means it'llwork regardless of where the code is executing. Same way using /images in the URL makes it work regardless of URL rewriting.
Link to comment
Share on other sites

Works like a charm but made me realize something new.

The desktop based images file size is too large for mobile.

I need to develope a code that will take all the images in that directory and either  make temporary smaller copies(best option for me) or make smaller copies for each new upload.

 

Thanks for the help :)

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.