Home >Backend Development >PHP Tutorial >PHP determines whether a file exists_PHP tutorial
php determines whether the file exists It is actually very simple to determine whether a file exists in PHP. It only requires one function: file_exists (file path); This function is generally used to determine whether a file exists, and then perform operations. Commonly used ones include deleting files: unlink (file path);
php tutorial to determine whether a file exists
It is actually very simple to determine whether a file exists in PHP. It only requires one function: file_exists (file path);
This function is generally used to determine whether the file exists and then operate it. Commonly used ones include deleting files: unlink (file path);
if(file_exists("data.txt"))
{
Print("This file exists"); //The file exists
Print(fileinode("data.txt"));
}
else
{
Print("File does not exist"); //File does not exist
}
Method 2
if($submit=="Find"){
$file_up=$_post[files];
if(file_exists($file_up)){
echo "File already exists!!";
}else{echo "The file does not exist!!";}
}
?>
Method 3
$filename = './d243375_0.png';
$filename = realpath($filename);
if (!file_exists($filename)) {
die("The picture does not exist~!");
}
$size = getimagesize ($filename);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if("image/png" != $size['mime'] || $file_extension != "png"){
die("This is not a complete png picture");
}
$img = @imagecreatefrompng ($filename);
if($img){
ob_start("output_handler");
imagepng($img);
ob_end_flush();
}else{
die("The png graphic cannot be created correctly, please check whether the png graphic is intact~");
}
function output_handler($img) {
header('content-type: image/png');
header('content-length:'.strlen($img));
return $img;
}
?>