JesseToxik Posted June 10, 2013 Share Posted June 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/ Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 Use absolute links. And please try to avoid using .. in URL paths if you can. echo ('');(the leading slash is what makes it absolute) Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435186 Share on other sites More sharing options...
JesseToxik Posted June 10, 2013 Author Share Posted June 10, 2013 Tried that. It doesn't even display a missing image. I tried all of the following possible paths: /images/gallery/ ../images/gallery/ images/gallery/ images/gallery http://mysite.com/images/gallery/ No luck with any of them. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435188 Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 http://cronanpilotcar.byethost33.com/images/gallery/Mustang.jpg Works for me. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435192 Share on other sites More sharing options...
JesseToxik Posted June 10, 2013 Author Share Posted June 10, 2013 By manually removing /page/? Of course. But when I let the server do it for me it includes it and my images do not load. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435198 Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 Then you're not using absolute links like I posted. What's your code now? Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435212 Share on other sites More sharing options...
JesseToxik Posted June 10, 2013 Author Share Posted June 10, 2013 <?php $files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE); foreach($files as $file) { echo ('<img src="'.$file.'">'); } ?> But no matter what I change the $files path to it has the same result. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435226 Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 Not only does that not look like what I posted, what I posted had nothing to do with $files. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435234 Share on other sites More sharing options...
JesseToxik Posted June 11, 2013 Author Share Posted June 11, 2013 (edited) Ah I missed that post >.> Edited June 11, 2013 by JesseToxik Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435432 Share on other sites More sharing options...
JesseToxik Posted June 11, 2013 Author Share Posted June 11, 2013 Well this is my code and it still isn't working :/ <?php $files = glob('/images/gallery/*.{jpg,png,gif}', GLOB_BRACE); foreach($files as $file) { echo ('<img src="/images/gallery/' . basename($file) . '">'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435433 Share on other sites More sharing options...
requinix Posted June 11, 2013 Share Posted June 11, 2013 Looks right to me. What's the source HTML of the page that's not working and what is the URL the image(s) should be pointing to? Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435442 Share on other sites More sharing options...
JesseToxik Posted June 11, 2013 Author Share Posted June 11, 2013 The page is http://cronanpilotcar.byethost33.com/page/photos and the image urls should appear as this one http://cronanpilotcar.byethost33.com/images/gallery/Mustang.jpg Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435443 Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 (edited) 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 June 12, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435464 Share on other sites More sharing options...
JesseToxik Posted June 12, 2013 Author Share Posted June 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435465 Share on other sites More sharing options...
JesseToxik Posted June 12, 2013 Author Share Posted June 12, 2013 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 / } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435467 Share on other sites More sharing options...
JesseToxik Posted June 12, 2013 Author Share Posted June 12, 2013 (edited) [DELETED] Edited June 12, 2013 by JesseToxik Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435469 Share on other sites More sharing options...
JesseToxik Posted June 12, 2013 Author Share Posted June 12, 2013 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 / } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435472 Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435486 Share on other sites More sharing options...
JesseToxik Posted June 12, 2013 Author Share Posted June 12, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279000-mod_rewrite-and-foreach-image-in-directory/#findComment-1435494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.