PLEASE HELP - need help with file_exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjmartinez
Forum Newbie
Posts: 2
Joined: Wed Oct 28, 2009 9:33 am

PLEASE HELP - need help with file_exists

Post by cjmartinez »

Ok, so I have been working on a project for the last week or so and have come to the point where I am completely stumped. I am by no means an expert or even an advanced php programmer, but, I have gained much knowledge in php over the last few months and learn more day by day. I usually find a way to figure out my own php problems/bugs but, I truly need someones help.

The script I have been working on makes a directory, copies two directories and two files into the directory it made, and then edits the two files copied, replacing certain strings with values that I assign through two variables in the script. I have the script tested and working. Here's where I get stumped...

I am setting up a cron job to run the script, but, I only want it to run if a specific file exists. I thought this would be easy as I would just have to do something like if(file_exists("blah.txt")){ Run Script }. This is the thing though, the file that the script checks for is created dynamically by a form submission, the form is named based on the value of a text box that the form posts.
Example; *-newfile.txt (the * being the value of the text box the form posted)

Every time the cron script finds a file with that naming convention I want it to loop through the script for each file it finds.

So basically, here's how it works so far:
1: user submits form with a text box filled in (example value : blah)
2: form posts to small script which creates local file called "blah-newfile.txt"
3: 3 cron checks if file exists (if I put the file name in manually like this if(file_exists("blah-newfile.txt")){ Run Script } it runs
4: cron uses - include('blah-newfile.txt'); (which contains the word they put into the form value) to insert the files contents into the value of a variable used in the cron. (example contents : "blah" - including quotes)

So basically the cron script has a first line that looks like $variable = include("blah-newfile.txt");

The problem I am having is writing something like: if(file_exists("*-newfile.txt")){ Run Script }

I have tried using glob as it feels as though this is the only solution for using a wildcard in a file_exists, but it doesnt seems to work and when I try and echo the variable it just says "Array to string conversion" as an error. So I try to do print readable on my array and it says that the value passed is not an array.

Here is a copy of the part of the script that checks if the file exists, I apologize for writing so much but wanted to make my problem perfectly clear:

$dir = "path/to/my/file/";
$newfile = glob($dir.'*-newfile.txt');
if(file_exists($newfile)){
foreach($newfile as $newfilerun) {

$variable = include($newfilerun);

RUN SCRIPT

}


Is there anything that anyone can see that I am doing wrong? If it is an easy solution, I apologize in advance for my stupidity :)

PLEASE, PLEASE someone help. If you see that I am wrong and respond, please paste an example or edit mine so that it will work or make recommendations on other ways of achieving this. Any help is GREATLY appreciated.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: PLEASE HELP - need help with file_exists

Post by Mark Baker »

Your path is relative to the script's current working directory.

What directory is the script currently running in?
Do an echo getcwd() to find out
cjmartinez
Forum Newbie
Posts: 2
Joined: Wed Oct 28, 2009 9:33 am

Re: PLEASE HELP - need help with file_exists

Post by cjmartinez »

Ok, for testing purposes (dont deem me stupid when you read this) I put it in the public directory that is the parent directory of where it creates the file:

example:
public_html ------
^-------tempdirectory
^---------------cron script location (directory)
^------------------------directory it creates the text file into.
Post Reply