I need an include
function / statement that will include a file only if it exists. Is there one in PHP?
You might suggest using @include
bu
@include($file);
Using at in front, ignores any error that that function might generate. Do not abuse this as IT IS WAY SLOWER than checking with if, but it is the shortest code alternative.
<?php
if(file_exists('file.php'))
include 'file.php';
}else{
header("Location: http://localhost/);
exit;
}
the @
suppresses error messages.
you cloud use:
$file = 'script.php';
if(file_exists($file))
include($file);
function get_image($img_name) {
$filename = "../uploads/" . $img_name;
$file_exists = file_exists($filename);
if ($file_exists && !empty($img_name)) {
$src = '<img src="' . $filename . '"/>';
} else{
$src = '';
}
return $src;
}
echo get_image($image_name);
Check out the stream_resolve_include_path function.
if(file_exists('file.php'))
include 'file.php';
That should do what you want