PHP: How to Check If Image File Exists

PHP: How to check if image file exists?

You need the filename in quotation marks at least (as string):

if (file_exists('http://www.mydomain.com/images/'.$filename)) {
… }

Also, make sure $filename is properly validated. And then, it will only work when allow_url_fopen is activated in your PHP config

Check if image file exist

Source : http://us1.php.net/file_exists

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>

OR

Source : http://us1.php.net/manual/en/function.is-file.php

<?php
var_dump(is_file('a_file.txt')) . "\n";
var_dump(is_file('/usr/bin/')) . "\n";
?>

Check if image file exists in another server

You can't use file_exists for a checking remote files.

Use get_headers function and check, if the file returns 200 or not.

$file_headers = get_headers('http://www.example.com/images/3.jpg');

if ($file_headers[0] == 'HTTP/1.1 200 OK') {
echo "The file exists";
} else {
echo "The file doesn't exist";
}

How do I test if an remote image file exists in php?

file_exists uses local path, not a URL.

A solution would be this:

$url=getimagesize(your_url);

if(!is_array($url))
{
// The image doesn't exist
}
else
{
// The image exists
}

See this for more information.

Also, looking for the response headers (using the get_headers function) would be a better alternative. Just check if the response is 404:

if(@get_headers($your_url)[0] == 'HTTP/1.1 404 Not Found')
{
// The image doesn't exist
}
else
{
// The image exists
}

How can i check if image file exists on smarty using URL?

In your controller, you should do something like:

if (file_exists($filename))

You could also do this in the view as you're trying to do now, but it's recommended to keep your logic separated from your view.

Don't forget to add the absolute path to the $filename variable.

Edit:

In case of smarty, you can do the following in your php script:

    $smarty = new Smarty();

$fileExist = false;
if (file_exists($filename)) {
$fileExists = true;
}

$smarty->assign('fileExists', $fileExists);

$smarty->display('index.tpl');

In the view/template, you can then simply do:

{if $fileExists}

Php check in dir if user picture exists

Here is how i solved it

    <?php 


$rootPath = $_SERVER['DOCUMENT_ROOT'];
$thisPath = dirname($_SERVER['PHP_SELF']);
$onlyPath = str_replace($rootPath, '', $thisPath);
$extension = ".png";
$all = $onlyPath.$_SESSION['username'].$extension;

if (file_exists($all)){

echo '<img style="margin-top:10px;" src="'.$onlyPath.$_SESSION['username'].'.png" width="200px;" height="200px;">';

} else {
echo '<img style="margin-top:10px;" src="usericon.png" width="200px;" height="200px;">';
}

?>

Other ways how to solve it

<?php 

$user = $_SESSION ["username"];
$dir = 'images/'.$user.'.png';

if (file_exists($dir)) {
//exists
} else {
//not exists
}

?>

How to check if image file inputed or not in PHP?

Replace this part of your PHP code

if(!empty($_FILES['profile_image'])){

#then execute this code when there is image uploaded
}else{
#execute this
}

with this

if (is_uploaded_file($_FILES ['profile_image'] ['tmp_name'])) {
#then execute this code when there is image uploaded
} else {
#execute this
}

And this part of your HTML code

<form  method='POST' action='update.PHP'>

with this

<form  method='POST' action='update.PHP' enctype='multipart/form-data'>

check if image exists php

Use getimagesize() to ensure that the URL points to a valid image.

if (getimagesize($imageURL) !== false) {
// display image
}


Related Topics



Leave a reply



Submit