0

why this wont work?

if ( file_exists($_SERVER['DOCUMENT_ROOT'].$dirfromroot)){
    echo "<img id='imagen' src='$imgloc' >";
}

else {
    echo 'No image to show!';
}

also tried

if ( file_exists($imgloc)){
    echo "<img id='imagen' src='$imgloc' >";
}

else {
    echo 'No image to show!';
}

Thanks!

3
Contributors
7
Replies
39
Views
3 Years
Discussion Span
Last Post by centenond
3

The code to check if the file exists is correct, so I am going to say that your path to the file that you're using is wrong.

This $_SERVER['DOCUMENT_ROOT'] is going to be the root directory for your hosted files. Unless the file exists in there (example: /home/public_html/) then this will fail. If the file is in the same directory that you're running this code above from, then you just need to put in the file name.

Votes + Comments
noice
0

i think the path its ok too here is the path

   $directorios = glob('images/verificarimg/{*.png,*jpg,*.jpeg,*.gif}', GLOB_BRACE);
foreach ($directorios as $numb=>$dirr) {
$dirfromroot = $dirr ;
}

so $dirfromroot echos this

images/verificarimg/img.jpeg

am i doing something wrong? Thanks for repply

1

I did this and got a list of file within my clientportal directory, try a forward slash in your path before images and make dirformroot an array.

foreach(glob('/www/ci/clientportal/{*.php,*.html,*.sql}',GLOB_BRACE) as $num => $dirr){
    $dirroot[] = $dirr;
}


var_dump($dirroot);
exit;

Results:
array (size=2)
0 => string '/www/ci/clientportal/index.php' (length=30)
1 => string '/www/ci/clientportal/cp_client_portal-barebones.sql' (length=51)

Edited by gabrielcastillo: show results from var dump

1

When I do this:

$path = $_SERVER['DOCUMENT_ROOT'] .'/'. $dirroot[0];
var_dump($path);
exit;

I get this:
string 'C:/www/ci/clientportal//www/ci/clientportal/index.php' (length=53)

So looks glob may not be what you are looking for. What is your goal you are trying to reach? Are you looking to find in an images exists within a directory? If so you could scan the directory and look for file's with .png, .jpg, .jpeg file extentions. PHP has other functions to read dir.

0

yes i would like to read all files images in a directory , how do i do the scan directory with extensions ?

Thanks gabriel

1

i did the slash and the array u told me , this is what worked in case some1 need it in the future

if ($_SERVER['DOCUMENT_ROOT'].'/'.$dirfromroot && !empty($dirfromroot))       {
echo "<img id='imagen' src='$imgloc' >";
}

else {
    echo 'No image to display!';
     }
Votes + Comments
Posting the resolution like you did is always helpful to others who may have the same problem.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.