473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

waiting for file to exist!

155 100+
can someone please help me set up a loop to wait for a file to be
exist?

i got a automation program that download a file from my ftp ,proccessing the file
and returning back the result as a file!

what im doing right now is im puting a sleep(15); to the executed file in my site to wait for the file!! ,the problem is that the proccesing of the file can take 1 second some time ,and some time 3 seconds, i put 15 seconds to be sure the file will be there!!

what can i do make my code wait for the file to be exist ,then continue!!

there is a way to put the sleep in a loop? like :

Expand|Select|Wrap|Line Numbers
  1. while (something)
  2. {
  3. if (file not exist) 
  4. { sleep(1) }
  5.  
  6. else {
  7.  
  8.  
  9. // continue
  10.  
  11.  
  12. }
  13.  
  14. }
thanx!!
Jul 6 '09 #1
23 13554
dlite922
1,584 Expert 1GB
what you're asking for is to create an "infinite" loop. replace "something" in your while loop with something that will always be true..obviously something like 1==1 or just simply the value "true".

This will eat up your memory though and won't be efficient. I suggest you put this in a scheduled job/crontab task and make it execute every so often. That means eliminate the loop, just create a PHP file that checks to see if a file is there, if not just exits gracefully; if yes, processes the file (and rename/removes it when done)

Any questions?





Dan
Jul 6 '09 #2
canabatz
155 100+
this is what i need ! ,i think this is the only way to do that ,i cannot tell the cron job all the details ,becuse the cron job is not reading my files!! ,if you can help me with my quastion that will be great! ,i need to put a sleep to my code!!
this is the way i think is best for my needs!!

thanx!!
Jul 6 '09 #3
dlite922
1,584 Expert 1GB
well a cron job wouldn't read your file, it would just call your php program, your php program deals with your file.

just do a cron job like this, changing the 15 to any minutes that you want:

Expand|Select|Wrap|Line Numbers
  1. 15 * * * * php -f /path/to/your/program.php
  2.  
and your code looks like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $yourInputFile = '/path/to/the/file.txt'; 
  4.  
  5. if(file_exists($yourInputFile)) {
  6.     // file exists, now do something with it; like spit it out:
  7.     $fileContents = file_get_contents($yourInputFile); 
  8.     echo $fileContents; 
  9. }
  10.  
  11. exit(); 
  12.  
  13.  
of course you won't see the result of this code because it is executed by the system in the background; you will not see its output.

if you execute the code from your command line, then you'll see it read the file.

Cheers,




Dan
Jul 6 '09 #4
zorgi
431 Expert 256MB
this one would run for 4 seconds only and will loop only 4 times...

Expand|Select|Wrap|Line Numbers
  1. $time = 4; //how long in seconds do you allow your program to run/search
  2. $found = false;
  3. for($i=0; $i<$time; $i++){
  4.  
  5.     if(file_exists('you/file/path/file.txt')){
  6.         //Found it
  7.         //do whatever
  8.                 $found = true;
  9.         break;
  10.     }
  11.     sleep(1); // if not found wait one second before continue looping
  12. }
  13.  
  14. if($found) echo "Found it";
  15.     else echo "Sorry you are timed out";
  16.  
Jul 7 '09 #5
canabatz
155 100+
this Exactly what i ask for!!

and this is exactly the answer !!!

thank you thank you!!

it's working perfect!!! :)
Jul 7 '09 #6
dlite922
1,584 Expert 1GB
Note:

It runs for 4 seconds but it also runs only 4 times. The time (period) is tied to the number of times (quantity) it runs.

It's not clear if this is what you wanted. I'm glad we helped you find what you need,

Cheers,




Dan!!!!!!!!!!!!!!!!!
Jul 7 '09 #7
canabatz
155 100+
here is how im using it:

Expand|Select|Wrap|Line Numbers
  1. $time = 15; //how long in seconds do you allow your program to run/search
  2. $found = false;
  3. for($i=0; $i<$time; $i++){
  4.  
  5.     if(file_exists('My_File')){
  6.         //Found it
  7.         //do whatever
  8.                 $found = true;
  9.         break;
  10.     }
  11.     sleep(1); // if not found wait one second before continue looping
  12. }
  13.  
  14. if($found) 
  15.  
  16. // here i got a long code !!
  17.  
  18.     else {
  19.  
  20.         $url="account.php?msg=3";echo '<script> window.top.location="' . $url . '"; </script>'; // msg = the system is unavilble!!
  21.  
  22.     }
i had it before with a sleep(15); as standart ,but it can take 1 second only!!

so i put now the time=15 so it will be there for a situation that the system is not available !!

im using this on credit card billing software that sit's in my computer!!

now it's working best ,traansaction happening in 1-4 second! :)

thank you all.
Jul 7 '09 #8
Markus
6,050 Expert 4TB
@canabatz
That's going to eat up your memory, and what happens when a transaction takes longer than 15 seconds?
Jul 7 '09 #9
canabatz
155 100+
it's taking max 5 seconds , normaly 1 sec ,i put it in case something is wrong!!

i got a dedicated server ,so i dont think it will be wrong to use this sleep!

do you have any other suggestions? i will be glad !

thanx
Jul 7 '09 #10
dlite922
1,584 Expert 1GB
I think we need to look a the big picture. Are you connecting to a merchant? How is this program used/executed? by the user from their desktop through your website's URL or is it a script that runs on your server.

I'm trying to know the scenario. Describe the goal of your application.

We may help you code something, but we might be leading you down the wrong path.



Dan
Jul 7 '09 #11
canabatz
155 100+
i got a program full of small utilities ,those utilities are programs to be in contact with the big Credit card company ,all transactions are going to this big company!


there is 1 main utility that is checking FILE1 and creating FILE2 ,FILE2 is the result of FILE1 !

i got a great automation program that is runing tasks ,like connect to ftp or reboot my machine if i tell it ! :)

the automation program is connecting to the ftp in my server and checking if there is FILE1 availible ,if it is then download this file ,after download the automation program is executing the MAIN utility ,the main utility after is executed is searching in his folder if FILE1 exist ,if the file is there ,this utility is conecting the big company and checking the credit card if is ok or not!! if it's good or bad all the data will be in a new file ,FILE2!
then the automation program is sending FILE2 to the server by ftp!!

thats why i need'd this code from first place! ,and like i said : before i ran the code in my site and wait for the FILE2 for minimum 15 seconds.

i hope you got me better now!! :)
Jul 7 '09 #12
dlite922
1,584 Expert 1GB
(I always get nervous when beginners deal with sensitive information like credit card processing)

How are you "telling it" to reboot your machine? I hope it's not through a URL parameter.

Why jump through so many hoops to connect to this BIG Credit card company. I thought BIG credit card companies had strict guidelines for their clients and most likely defined a very specific way to interact with their API.

Nevertheless, your program will still fail if network traffic is heavy and the script takes more than 15 seconds. What happens then? You need to provide a timeout. half an hour? Your server should provide a response (ie comeback with something instead of nothing) at least to let the execution continue.



Dan
Jul 7 '09 #13
canabatz
155 100+
reboot my machine if i tell it ! :)
Jul 7 '09 #14
canabatz
155 100+
the script is runing for maximum 5 seconds!!
in five seconds it will eat the memory?
Jul 7 '09 #15
canabatz
155 100+
Expand|Select|Wrap|Line Numbers
  1. $time = 15; //how long in seconds do you allow your program to run/search
  2. $found = false;
  3. for($i=0; $i<$time; $i++){
  4.  
  5.     if(file_exists('My_File')){
  6.         //Found it
  7.         //do whatever
  8.                 $found = true;
  9.         break;
  10.     }
  11.     sleep(1); // if not found wait one second before continue looping
  12. }
  13.  
  14. if($found) 
  15.  
  16. // here i got a long code !!
  17.  
  18.     else {
  19.  
  20.         $url="account.php?msg=3";echo '<script> window.top.location="' . $url . '"; </script>'; // msg = the system is unavilble!!
  21.  
  22.     }
Jul 7 '09 #16
dlite922
1,584 Expert 1GB
With all the exclamation marks!!!! I don't know what's a joke and what's not!!!

here's some more --> !!!! :)

And no, it won't eat memory. The reason I'm concerned is that the reason why PHP is known as "less" secure is because it allows beginners to make beginner mistakes. Nothing against you personally, I've been there, we all have. I'm just trying to reduce that as I help people here on this forum.

I hope your code works great, is secure, and you have fun coding!!!!!! !! !!


Dan

!!!!
Jul 7 '09 #17
Markus
6,050 Expert 4TB
@canabatz
It's definitely not efficient, no.

Also, for the last time, what happens if it takes longer than 15 seconds?
Jul 7 '09 #18
canabatz
155 100+
Marcus!

this code is breaking after 15 sec.

if the file not found ,the user is geting the massage that the credit card proccesing system is unavailable.

thats it.
Jul 7 '09 #19
canabatz
155 100+
Hi friends ,long time no see :)

after using your help on file_exist ,and thnx to all of you!!

now im trying to do the oposite ,i want to wait for file to not exist!!

this is the working code im using right now to wait for file to exist:
Expand|Select|Wrap|Line Numbers
  1. $time = 15; //how long in seconds do you allow your program to run/search
  2. $found = false;
  3. for($i=0; $i<$time; $i++){
  4.  
  5.     if(file_exists('INT_OT')){
  6.         //Found it
  7.         //do whatever
  8.                 $found = true;
  9.         break;
  10.     }
  11.     sleep(1); // if not found wait one second before continue looping
  12. }
  13.  
  14. if($found) echo "Found it";
  15.     else echo "Sorry you are timed out";

i need small help to change it to not_exist :)

any help will be very very appreciated!!

thanx
Sep 1 '09 #20
Markus
6,050 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. if(!file_exists('/path/to/file')) {
  2.     // File doesn't exist
  3. }
  4.  
Simple enough, no?
Sep 1 '09 #21
canabatz
155 100+
can i put it in the loop i posted?

the file can be exist and delted after by the rest of the code

so what i need is to wait for the file to not exist ,if it is exist continue with the loop until it is not exist ,if it is not exist continue the rest of the code.

thanx Markus
Sep 1 '09 #22
canabatz
155 100+
Expand|Select|Wrap|Line Numbers
  1. $time = 15; //how long in seconds do you allow your program to run/search
  2. $found = true;
  3. for($i=0; $i<$time; $i++){
  4.  
  5.     if(!file_exists('INT_OT')){
  6.  
  7.         echo "the file is not there any more";
  8.                 $found = false;
  9.         break;
  10.     }
  11.     sleep(1); // if not found wait one second before continue looping
  12. }
Sep 1 '09 #23
canabatz
155 100+
thanx Markus!................................
Sep 1 '09 #24

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

Similar topics

9
by: Oliver Douglas | last post by:
Mr. Jeff Relf drawled... > You dream of a world where everyone shares their source code , > sibling node to sibling node . > What if it's best the way things are , > With closed source...
1
by: Bartje | last post by:
Hey, I am wondering what the best solution will be to program the following problem in access, the dutch 97 edition. I am developing a database for a day care centre (for my girlfriend). This...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
12
by: Raymond Lewallen | last post by:
How to wait for a process to stop completion is my goal. Obviously, the looping while waiting for the HasExited property is not a solution.. but thats the best I can come up off the top of my...
4
by: Mike | last post by:
Hi, I am looking for function in .Net library that let me know if exist any file if I specified template. Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then I can get True...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
0
by: =?Utf-8?B?QmVu?= | last post by:
Hello, I need to use the functionality of an AutoResetEvent in that it should normally be non-signalled, but I want something to be able to release all the waiting threads when I call Set, however...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.