Reputation: 13
I scan the folder to display all the pictures from it. How to set the folder absolute path?
Pictures are in the folder //localhost/img/gallery/.
There are two index.php files. The first - in the root directory. The second is in the folder "en". In the first, the function works well. In the second function adds the folder "en" to the url. And instead of "localhost/img/gallery/" - it turns out "localhost/en/img/gallery/". If you add the site address to the directory, the path is correct. But an error appears: "Warning: scandir (//localhost/img/gallery/): failed to open dir: not implemented".
// in function.php:
function GetImages($path){
$wimg = "";
$fimg = "";
$images = scandir($path);
if ($images !== false) {
$images = preg_grep("/\.(?:png|gif|jpe?g)$/i", $images);
if (is_array($images)) {
foreach($images as $image) {
$fimg .= '
<a href="'.$path.htmlspecialchars(urlencode($image)).'">
<img src="'.$path.htmlspecialchars(urlencode($image)).'">
</a>
';
}
$wimg .= $fimg;
} else {
$wimg .= "<p>No images in directory!</p>";
}
} else {
$wimg .= "<p>Error.</p>";
}
echo $wimg;
}
// in index.php:
<?php GetImages("img/gallery/");?> // it's Ok
// in en/index.php:
<?php GetImages("img/gallery/");?> // it turns out "localhost/en/img/gallery/". But there are no pictures.
// if in en/index.php:
<?php GetImages("localhost/img/gallery/");?> // it turns out "Warning: scandir(//localhost/img/gallery/): failed to open dir: not implemented in D:\OpenServer\domains\localhost\function.php on line XXX"
So an error too:
// in function.php
function GetImages(){
$wimg = "";
$fimg = "";
$path = $_SERVER['SERVER_NAME'] . "/img/gallery/";
// in index.php and in en/index.php
<?php GetImages();?> // "Warning: scandir(localhost/img/gallery/,localhost/img/gallery/): ������� �� ������ ����� �������� ���. (code: 3) in D:\OpenServer\domains\localhost\function.php on line XXX"
but
// in function.php
function GetImages(){
$wimg = "";
$fimg = "";
$path = "/img/gallery/";
// in index.php
<?php GetImages();?> // it's Ok
// in en/index.php
<?php GetImages();?> // it turns out "localhost/en/img/gallery/". But there are no pictures.
dirname(FILE) . is error too.
Upvotes: 0
Views: 7093