Last active
December 18, 2016 12:42
-
-
Save daslicht/b7410a501414d08509a595651f099a52 to your computer and use it in GitHub Desktop.
Return Image URL form a directory with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Der Image Pfad auf dem server relative zu diesem Script | |
*/ | |
$image_directory = "./images/"; | |
/** | |
* Die base URL fuer die Bilder | |
*/ | |
$base_url = "http://localhost:8000/images/"; | |
/** | |
* Lese das Dir aus und speicher es in einem Array: | |
*/ | |
$directory_listing = scandir($image_directory); | |
/** | |
* var_dump zeigt dir ein array im browser an zum debuggen | |
*/ | |
var_dump( $directory_listing ); | |
/** | |
* Iteriere ueber das Array | |
* $key ist der index , startet bei 0 | |
* $directory die jeweilige Zelle? des Arrays | |
*/ | |
foreach ( $directory_listing as $key => $directory ) { | |
if ( $key > 1 ) { // das Directory Listing beginnt immer . .. also starten wir erst die Ausgabe ab 2 :) | |
$result = ""; // Hier speichern temporaer wir die URL des Images | |
$result = $base_url . $directory; // hier verknuepfen wir 2 Strings , i=n JS waere es einfach a + b, in PHP a . b | |
// In der naechten Zeile verlassen wir PHP und geben einfach HTML aus : | |
?> | |
<a href="<?php echo $result ?>" data-toggle="lightbox"> <!-- Hier geben wir die obene definierte PHP Variable mittels Echo aus --> | |
<img src="<?php echo $result ?>" class="img-fluid"> | |
</a> | |
<?php | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment