473,788 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file_exists() question.

66 New Member
Hi, I need to check if a file with a certain string in its filename exists. How would I do that?
Dec 2 '07 #1
4 1125
ak1dnar
1,584 Recognized Expert Top Contributor
what is your desired output?
Just show us some code, that will give us a clue.
Dec 2 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Cheesecaker.

Check out glob().
Dec 2 '07 #3
cheesecaker
66 New Member
Yeah, I tried glob, but it's very inefficient. So I used a looping readdir();

Here's the function. Works for my circumstances, don't know about anyone else's:

[PHP]/** Checks whether a file with the specified string in its name
* exists in the specified directory.
* Returns 1 if any matching file is found, 0 if not.
*/

function file_exists_con taining ($directory, $string) {
$dirhandle = opendir($direct ory);
$exists = 0;
while($file = readdir($dirhan dle)) {
if (strstr($file, $string)) {
$exists = 1;
break;
}
}
return $exists;
}[/PHP]
Dec 3 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, Cheesecaker.

You may find scandir() useful here.
Dec 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3760
by: Andrew Crowe | last post by:
Hi guys, I've created this little function to check whether a user has uploaded a file with the same name as an existing file, and if so rename it to file-1.jpg, file-2.jpg etc. clearstatcache(); if(file_exists("$BasePath/$FileName.jpg")){ $extra=1; while(file_exists("$BasePath/$FileName-$extra.jpg")){
10
3999
by: Google Mike | last post by:
{NOTE: I have PHP 4.2.2 for RH9 Linux.} Anyone have a better file_exists() out there? Even if you use shell out tricks with Linux using the `command` trick, I'd be interested to see what you came up with. I have this remotely mounted SMB shares on Linux, stuck in my /mnt directory, and the docs tell me that file_exists() doesn't work on that. When I tested this, I found this to be true. Therefore, I need an alternative.
1
2285
by: Hinrich Specht | last post by:
Hello, I have a problem using file_exists. I want to use file_exists to dertermine if a product-image is available or not to show either the product-image or a standard-image. This is the code: $image = "../../artimages/".$item_code.".jpg"; if (file_exists($image))
3
7970
by: annie | last post by:
Hi I've got two linux boxes running redhat9 and either PHP Version 4.2.2 or PHP Version 4.3.7. The file structure is set up the same on both machines and both files exist. The problem is not file specific and on both machines seq_tables is a symbolic link to another location. It makes no difference to either version of php whether I use double or single quotes when asigning a value to $filename. Nor does it matter whether I use...
5
8714
by: lkrubner | last post by:
I've written some template code and one thing I'm trying to protect against is references to images that don't exist. Because users have the ability to muck around with the templates after everything's been set up, there is a chance they'll delete an image or ruin some tag after the web designer has set up everything perfectly. I want the software to catch mistakes like that and, at the very least, not show broken links. On the control...
2
1272
by: julian_m | last post by:
I've a site working as follows Table "articles" When somebody want to see the article's details, I generate (thorugh php) a page where all the detalis are listed, as well as the article's views (images)
6
2786
by: +86 | last post by:
i encountered this problem: "include('inc.php')" will work problely but "include('./inc.php') doesn't work .. both file_exists('inc.php') or file_exists('./inc.php') didn't return the right value.this always show "file doesn't exists' my environment is win2003+iis+php5 how to fix it ?
20
3668
by: Bob Sanderson | last post by:
This is my code: if (file_exists($Fname)) { echo "<td>$Fname exists</td>"; } else { echo "<td>$Fname does not exist</td>"; } $Fname is the full path to the file I'm trying to verify. When I run the script, I get the following output:
9
2131
by: damezumari | last post by:
$file = 'C:\Documents and Settings\Michael\Escritorio\Elvis\test.txt'; file_exists($file) and file_exists('"'.$file.'"') both return false although the file does exist on the user's machine. What am I doing worng?
3
2070
by: rickcasey | last post by:
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need to detect if a file exists, and if so, unlink it. I am mystified as to why the file_exists() function does not work in one particular section of code, yet does work in another section. The unlink() function similarly works in one section, but not the other. I have read other posts in this group where others have had trouble with the file_exists() function, but none...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.