简体   繁体   中英

getimagesize return array even the file doesn't exists

I have images with user id (eg. img299 etc.). This images are already place in a folder called images . When user login, I create image name with user id like this img299 ( 299 is the user id).

After I create image name, I need to check this image name is already exists or not in images folder. If image is exists, I want to show that image and if not just only show underline.

So, I try like this:

$filename = "img".$userID;
$filepath = "http://www.example.com/images/".$filename.".gif";

var_dump(getimagesize($filepath));//for testing

if(getimagesize($filepath)) {
    echo "<img src = 'http://www.example.com/images/".$filename.".gif'>";                              
} else {
    echo "____________________________";
}

The above code is work if the image is exists. But the problem is, if the image is not exists under images folder, getimagesize return like this:

getimagesize返回数组

So, the result is true and that line echo "http://www.example.com/images/".$filename.".gif'>"; is work and image can't show correctly.

That is the problem for me. I want to show only underline if the image is not exists.

So, I also try with if(file_exists($filepath)) and it always return false even if the image is exists.

How can I check image is exists or not. I will appreciate any suggestion.

Use PHP's file_exists with the local path to the file to check if it exists. Ie do not use the full URL (example.com/...) but the path on the filesystem relative to the file you're executing (eg ../images/filename.png ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM