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!

Recommended Answers

All 7 Replies

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.

commented: noice +10

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

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)

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.

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

Thanks gabriel

Hey gabriel i tried the slash... IT WORKED! THANKS!!! U ROCK!

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!';
     }
commented: Posting the resolution like you did is always helpful to others who may have the same problem. +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.