check if url exists in php
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
Is this the correct way of checking if a file exists?
If you want to break the loop to add a break statement
after unlink()
function as:
foreach (glob("../imgs/" . $name . ".*") as $file) {
if (file_exists($file)) {
unlink($file);
break;
}
}
If you want to check PHP file exists only:
You can use the file_exist()
function. This function returns TRUE
if the file exists, otherwise, it returns FALSE
.
$filename = './file_name.txt';
if(file_exists($filename)){
echo sprintf('file %s exists',$filename);
}else{
echo sprintf('file %s does not exist',$filename);
}
If you want to check PHP file exist and readable:
You can use the is_readable()
function. The function returns TRUE
if the file exists and is readable, otherwise, it returns FALSE
.
$filename = './file_name.txt';
if(is_readable($filename)){
echo sprintf('file %s exists and readable',$filename);
}else{
echo sprintf('file %s does not exist or is not readable',$filename);
}
If you want to check PHP file exists and writable:
You can use the is_writable()
function. The function returns TRUE
if the file exists and is writable, otherwise it returns FALSE
.
$filename = './file_name.txt';
if(is_writable($filename)){
echo sprintf('file %s exists and writable',$filename);
}else{
echo sprintf('file %s does not exist or is not writable',$filename);
}
Checking if file exists in directory
You can use this code to skip the .
and ..
results from the scandir
result:
<?php
$files = array_filter(scandir('/var/log/phplogs/iplogs/'), function($item) {
return $item !== '.' && $item !== '..';
});
foreach ($files as $filename)
{
if(file_exists("/var/log/phplogs/iplogs/$filename"))
{
echo "file exist\n";
}
else
{
echo "file doesn't exist\n";
}
}
By modifying the array_filter
callback, you can also leave out other files and even subdirectories.
PHP check if file not exists in any directory
You need to wait until the end of the loop to know if the file was not found in any of the directories.
And continue;
should be break;
. continue
restarts starts the next iteration of the current loop, but once you find the file you want to exit the loop completely.
foreach($files as $file){
$found = false;
foreach ($folders as $this_folder) {
if (file_exists($this_folder.$file)) {
$exists[] =$file;
$found = true;
break;
}
}
if (!$found) {
// What to do if file is not exist in any directory, add into array.
$notexists[] = $file;
}
}
PHP if file exists
Just try to use like this:
$filename = dirname(__FILE__) . "/ $myMedia catalog/category/ $myImage.png";
$filename = str_replace(" ", "", $filename);
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Best way to check if a file exists before execute a file_put_contents function
To check if file exists you can use built up function file_exists in php.
Example :
if (file_exists($file)) {
//file exists
}
Alternatively you can use get_headers function.
Example:
$headers = @get_headers($filename);
$exists = ($headers[0] == 'HTTP/1.1 404 Not Found'); //file does not exist
How to Check file exists in Laravel
add one more additional condition to check given path is file but not a directory
public function file_fetch(Request $request) {
$file = request('routename');
$destinationPath = public_path('/folder/'.$file);
if(!File::exists($destinationPath) && !is_dir($destinationPath)){
$content = File::get($destinationPath);
return view('filefetch', compact('file','content'));
}
else {
return redirect('/')->witherrormessage('NO such File Exists');
}
}
php check if file exist on multiple folders at the same time
I'll agree with @arkascha 's view that if the file is for internal use, using file name with unique hash will be better.
But if it's necessary,
Try use foreach() to check those prefix in your array at once.
function files_suffix_exists($file_name, $folders){
$root = $_SERVER["DOCUMENT_ROOT"];
if(file_exists($root."/".$file_name . '.' . $extension){
return true;
}
foreach($folders as $folder){
if(file_exists($root."/".$folder."/".$file_name . '.' . $extension){
return true;
}
}
return false;
}
$folders[] = "thumb";
$folders[] = "medium";
$folders[] = "large";
$folders[] = "xlarge";
$increment = ''; //start with no suffix
while(files_suffix_exists($file_name, $folders))
$increment++;
$file_name .= $increment;
}
$final_name = $file_name . '.' . $extension;
Related Topics
Root Path Doesn't Work with PHP Include
Verify Valid Date Using PHP's Datetime Class
Codeigniter Echoing [::1] Instead of Localhost
Which Is the Best Character Encoding for Japanese Language for Db, PHP, and HTML Display
How to Decode Numeric HTML Entities in PHP
Create Superglobal Variables in PHP
Skip Checkout in Magento for a Downloadable Product
How to Prevent JavaScript Code Theft
Cannot Figure Out How to Run a MySQLi_Multi_Query and Use the Results from the Last Query
How to Validate Phone Number Using PHP
Connecting to Websocket with PHP Client
How to Get the Server Path to the Web Directory in Symfony2 from Inside the Controller
How to Embed HTML Files in PHP Code
Check If a File Was Included or Loaded
Using '$This' in an Anonymous Function in PHP Pre 5.4.0
Mask Credit Card Number in PHP
Http://Localhost/ Not Working on Windows 7. What's the Problem