469,270 Members | 1,146 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

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 12158
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

Post your reply

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

Similar topics

9 posts views Thread by Oliver Douglas | last post: by
12 posts views Thread by Raymond Lewallen | last post: by
4 posts views Thread by Mike | last post: by
52 posts views Thread by paytam | last post: by
reply views Thread by =?Utf-8?B?QmVu?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.