


The following is a simple example code to check whether the file exists:
Copy the code The code is as follows:
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
If the file exists, the displayed result of executing the PHP file is:
The file C:blablaphphello.txt exists.
If the file does not exist, the displayed result of executing the PHP file is:
The file C:blablaphphello.txt does not exist.
You can also use the file_exists function to test whether a directory exists, sample code As follows:
Copy code The code is as follows:
if (file_exists("C:blablaphp"))
{echo "yes";}
else
{echo "no";}
Example
Copy code The code is as follows:
/* *
* File or directory permission check function
*
* @access public
* @param string $file_path File path
* @param bool $rename_prv Whether to check the permission to execute the rename() function when checking modification permissions
*
* @ return int The value range of the return value is {0 * The return value is in binary notation, and the four digits from high to low respectively represent
* permissions to execute the rename() function, permissions to append content to files, permissions to write files, and permissions to read files.
*/
function file_mode_info($file_path)
{
/* If it does not exist, it is unreadable, unwritable, and unchangeable*/
if (!file_exists($file_path))
{
return false;
}
$mark = 0;
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
{
/* Test file*/
$test_file = $file_path . '/cf_test.txt';
/* If it is a directory*/
if (is_dir($file_path))
{
/* Check whether the directory is readable*/
$dir = @opendir($file_path);
if ($dir === false)
{
return $mark; //If the directory opening fails, return directly to the directory which cannot be modified, written or read
}
if (@readdir($dir) !== false)
{
$mark ^= 1; //The directory is readable 001, the directory is unreadable 000
}
@closedir($dir);
/* Check whether the directory is writable*/
$fp = @fopen($test_file, 'wb');
if ($ fp === false)
{
return $mark; //If the file in the directory fails to be created, it returns unwritable.
}
if (@fwrite($fp, 'directory access testing.') !== false)
{
$mark ^= 2; //The directory is writable and readable 011, the directory is writable and unreadable 010
}
@fclose($fp);
@unlink($test_file);
/* Check whether the directory can be modified*/
$fp = @fopen($test_file, 'ab+');
if ($fp === false)
{
return $mark;
}
if (@fwrite($fp, "modify test.rn") !== false)
{
$mark ^= 4;
}
@fclose($fp);
/* Check whether the directory has permission to execute the rename() function*/
if (@rename($test_file, $test_file) !== false)
{
$mark ^= 8;
}
@unlink($test_file );
}
/* If it is a file*/
elseif (is_file($file_path))
{
/* Open in read mode*/
$fp = @fopen($file_path, 'rb');
if ( $fp)
{
$mark ^= 1; //Readable 001
}
@fclose($fp);
/* Try to modify the file*/
$fp = @fopen($file_path, 'ab+') ;
if ($fp && @fwrite($fp, '') !== false)
{
$mark ^= 6; //Can be modified, written and read 111, cannot be modified, written and read 011...
}
@fclose($fp);
/* Check whether the directory has permission to execute the rename() function*/
if (@rename($test_file, $test_file) !== false)
{
$mark ^ = 8;
}
}
}
else
{
if (@is_readable($file_path))
{
$mark ^= 1;
}
if (@is_writable($file_path))
{
$mark ^ = 14;
}
}
return $mark;
}
PHP determines whether the directory exists
Copy code The code is as follows:
/*****************************************************
* Write xml data stream to xml file
* @param $xmlData
* @return bool|string
*/
function writeXmlFile($xmlData)
{
$time = time (); //Get the timestamp, used to name the file
$path = dirname(__FILE__); //Get the current absolute path
$path = substr_replace($path, "", stripos($path, "actionsdata")); //Replace the inherent path of this file with an empty one
$path .= "xmlFiles"; //Storage directory name
/ *Determine whether the target directory exists, create a new one if it does not exist*/
if(!is_dir($path))
{
mkdir($path); //Create a new directory
}
/*Record the complete path and file name*/
$filePathAndName = $path.$time.".xml";
/*Open the file, the file name is
$fp = fopen($filePathAndName, "w") ;
if(!$fp)
{
return false;
}
/*Write to file stream*/
$flag = fwrite($fp, $xmlData);
if(!$flag)
{
return false ;
}
fclose($fp);
return $filePathAndName;
}

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
