Discussion:
for loop not working
hadi
2014-06-14 18:54:21 UTC
Permalink
Hi ,

Can someone help me to fix my code. It throwing error. I couldn't determine
how to do the for loop.

Here is my code

<?php


for ($i; in /tmp/sess*)

{


if (time()- filemtime($ip) > 60)

{

$old = 'hadi';

$filename = '$i';

$file = file_get_contents($filename);

strpos($file, $old);

unlink ($filename);

break();

}
}

?>
Paladin
2014-06-14 18:59:34 UTC
Permalink
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't determine
how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use glob and loop over it's result

you will want something like

<?php

$files = glob('/tmp/sess*')

foreach ($files as $file) {
// do something with file $file
}

Hope it'll help,
Paladin
--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.
hadi
2014-06-14 19:19:14 UTC
Permalink
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying

(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6

Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)


$files = glob('/tmp/sess*')

foreach ($files as $file)

{

if (time()- filemtime($ip) > 60)

{

$old = 'hadi';

$filename = '$file';

$file1 = file_get_contents($filename);

strpos($file1, $old);

unlink ($filename);

break();

}
}

?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
Ashley Sheridan
2014-06-14 19:23:46 UTC
Permalink
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
You've got the order of your foreach arguments reversed. You should use the online docs to avoid these sorts of issues.
Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jasper Kips
2014-06-14 19:26:40 UTC
Permalink
Try putting a semicolon after: $files = glob('/tmp/sess');

Verstuurd vanaf mijn iPad
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tomas Volf
2014-06-14 19:27:31 UTC
Permalink
You're missing ; after glob function.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
hadi
2014-06-14 19:47:09 UTC
Permalink
Post by Tomas Volf
You're missing ; after glob function.
Thank all for tilling me putting ; after the function.

Im getting an there error

PHP Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18

Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18
PHP Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22

Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22

But The file exists im sure about it.
Post by Tomas Volf
You're missing ; after glob function.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Ashley Sheridan
2014-06-14 19:49:19 UTC
Permalink
Post by hadi
Post by Tomas Volf
You're missing ; after glob function.
Thank all for tilling me putting ; after the function.
Im getting an there error
PHP Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18
Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18
PHP Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
But The file exists im sure about it.
Post by Tomas Volf
You're missing ; after glob function.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I
couldn't
Post by Tomas Volf
Post by hadi
Post by Paladin
Post by hadi
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Does the user your script is running as (usually apache or www) have read access to those files?
Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
hadi
2014-06-14 20:00:38 UTC
Permalink
Does the user your script is running as (usually apache or www) have read
access to those files?

Its running on local host. As apache.
-----Original Message-----
Sent: Saturday, June 14, 2014 10:49 PM
To: hadi; 'Tomas Volf'; 'Jasper Kips'; 'Paladin'
Subject: RE: [PHP] for loop not working
Post by hadi
Post by Tomas Volf
You're missing ; after glob function.
Thank all for tilling me putting ; after the function.
Im getting an there error
PHP Warning: file_get_contents($file): failed to open stream: No such
file or directory in /var/www/html/phptest/search2.php on line 18
Warning: file_get_contents($file): failed to open stream: No such file
or directory in /var/www/html/phptest/search2.php on line 18 PHP
Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
But The file exists im sure about it.
Post by Tomas Volf
You're missing ; after glob function.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I
couldn't
Post by Tomas Volf
Post by hadi
Post by Paladin
Post by hadi
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Does the user your script is running as (usually apache or www) have read
access to those files?
Thanks,
Ash
Jim Giner
2014-06-14 20:10:05 UTC
Permalink
Post by hadi
Im getting an there error
PHP Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18
Warning: file_get_contents($file): failed to open stream: No such file or
directory in /var/www/html/phptest/search2.php on line 18
PHP Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
Warning: unlink($file): No such file or directory in
/var/www/html/phptest/search2.php on line 22
Post by Tomas Volf
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
This would be a perfect time to tell you to begin using error checking
throughout your code. If you had CHECKED the result of the first
operation you would have avoided all the remaining errors. Good practice!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Shawn McKenzie
2014-06-14 20:01:59 UTC
Permalink
Yes, you need to have a rudimentary understanding of PHP.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file
}
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
hadi
2014-06-14 20:10:19 UTC
Permalink
Post by Shawn McKenzie
Yes, you need to have a rudimentary understanding of PHP.
Thank you,

Can you ably to fix my code please?
Post by Shawn McKenzie
-----Original Message-----
Behalf Of Shawn McKenzie
Sent: Saturday, June 14, 2014 11:02 PM
To: hadi
Subject: Re: [PHP] for loop not working
Yes, you need to have a rudimentary understanding of PHP.
Post by hadi
Paladin,
Post by Paladin
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
It's not working it giving error check my code after modifying
(PHP Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6
Parse error: syntax error, unexpected T_FOREACH in
/var/www/html/phptest/search2.php on line 6)
$files = glob('/tmp/sess*')
foreach ($files as $file)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$file';
$file1 = file_get_contents($filename);
strpos($file1, $old);
unlink ($filename);
break();
}
}
?>
Post by Paladin
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
[..]
Hi, take a look at glob function.. you cannot loop over filesystem
directly. use
Post by Paladin
glob and loop over it's result
you will want something like
<?php
$files = glob('/tmp/sess*')
foreach ($files as $file) {
// do something with file $file }
Hope it'll help,
Paladin
--
cache invalidation, naming things and off-by-one errors.
Shawn McKenzie
2014-06-14 19:49:31 UTC
Permalink
You'll need to outline exactly what you are trying to do as that is barely
PHP code and makes little sense. Have you heard of http://www.php.net?
Here is a best guess to delete files modified more than 60 seconds that
contain "hadi":

foreach(glob('/temp/sess/*') as $file) {
if(time() - filemtime($file) > 60) {
$old = 'hadi';
$content = file_get_contents($file);
if(strpos($content, $old) !== false) {
unlink($file);
}
}
}

-Shawn
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't determine
how to do the for loop.
Here is my code
<?php
for ($i; in /tmp/sess*)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$i';
$file = file_get_contents($filename);
strpos($file, $old);
unlink ($filename);
break();
}
}
?>
hadi
2014-06-14 20:06:53 UTC
Permalink
Post by Shawn McKenzie
You'll need to outline exactly what you are trying to do as that is barely
PHP
code and makes little sense. Have you heard of http://www.php.net?
Here is a best guess to delete files modified more than 60 seconds that
Im not trying to delete file more than 60 second I just want my code to be
fixed.

I just wanted to delete the file which contain the value "hadi"
Post by Shawn McKenzie
-----Original Message-----
Behalf Of Shawn McKenzie
Sent: Saturday, June 14, 2014 10:50 PM
To: hadi
Subject: Re: [PHP] for loop not working
You'll need to outline exactly what you are trying to do as that is barely
PHP
code and makes little sense. Have you heard of http://www.php.net?
Here is a best guess to delete files modified more than 60 seconds that
foreach(glob('/temp/sess/*') as $file) {
if(time() - filemtime($file) > 60) {
$old = 'hadi';
$content = file_get_contents($file);
if(strpos($content, $old) !== false) {
unlink($file);
}
}
}
-Shawn
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
<?php
for ($i; in /tmp/sess*)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$i';
$file = file_get_contents($filename);
strpos($file, $old);
unlink ($filename);
break();
}
}
?>
Jim Giner
2014-06-14 20:16:34 UTC
Permalink
Post by hadi
Im not trying to delete file more than 60 second I just want my code to be
fixed.
Post by hadi
if (time()- filemtime($ip) > 60)
{
You don't make sense. If you aren't interested in a 60 second check
then what is that (useless) line above even in the code for?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
hadi
2014-06-14 20:24:19 UTC
Permalink
You don't make sense. If you aren't interested in a 60 second check then
what is that (useless) line above even in the code for?
This for : I have file name ip if this file is less than 60 second delete the
"/tmp/sess*"
Where only "hadi" value there.

I hope you
Understand it.
-----Original Message-----
Sent: Saturday, June 14, 2014 11:17 PM
Subject: Re: [PHP] for loop not working
Post by hadi
Im not trying to delete file more than 60 second I just want my code
to be fixed.
Post by hadi
if (time()- filemtime($ip) > 60)
{
You don't make sense. If you aren't interested in a 60 second check then
what is that (useless) line above even in the code for?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-06-14 20:31:02 UTC
Permalink
Post by hadi
You don't make sense. If you aren't interested in a 60 second check then
what is that (useless) line above even in the code for?
This for : I have file name ip if this file is less than 60 second delete the
"/tmp/sess*"
Where only "hadi" value there.
I hope you
Understand it.
-----Original Message-----
Sent: Saturday, June 14, 2014 11:17 PM
Subject: Re: [PHP] for loop not working
Post by hadi
Im not trying to delete file more than 60 second I just want my code
to be fixed.
Post by hadi
if (time()- filemtime($ip) > 60)
{
You don't make sense. If you aren't interested in a 60 second check then
what is that (useless) line above even in the code for?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Do you have SSH access on the server (command prompt style)?
hadi
2014-06-15 17:21:43 UTC
Permalink
Post by Aziz Saleh
Do you have SSH access on the server (command prompt style)?
Its running in my local machine. Don’t bother I got the code working fine. Thank you for your asking.
Post by Aziz Saleh
-----Original Message-----
Sent: Saturday, June 14, 2014 11:31 PM
To: hadi
Subject: Re: [PHP] for loop not working
Post by hadi
Post by Jim Giner
You don't make sense. If you aren't interested in a 60 second check
then what is that (useless) line above even in the code for?
This for : I have file name ip if this file is less than 60 second
delete the "/tmp/sess*"
Where only "hadi" value there.
I hope you
Understand it.
Post by Jim Giner
-----Original Message-----
Sent: Saturday, June 14, 2014 11:17 PM
Subject: Re: [PHP] for loop not working
Post by hadi
Im not trying to delete file more than 60 second I just want my
code to be fixed.
Post by hadi
if (time()- filemtime($ip) > 60)
{
You don't make sense. If you aren't interested in a 60 second check
then what is that (useless) line above even in the code for?
--
PHP General Mailing List (http://www.php.net/) To unsubscribe,
visit: http://www.php.net/unsub.php
Do you have SSH access on the server (command prompt style)?
hadi
2014-06-14 20:20:36 UTC
Permalink
Post by Shawn McKenzie
foreach(glob('/temp/sess/*') as $file) {
if(time() - filemtime($file) > 60) {
$old = 'hadi';
$content = file_get_contents($file);
if(strpos($content, $old) !== false) {
unlink($file);
}
}
}
Your code working think you.
Post by Shawn McKenzie
-----Original Message-----
Behalf Of Shawn McKenzie
Sent: Saturday, June 14, 2014 10:50 PM
To: hadi
Subject: Re: [PHP] for loop not working
You'll need to outline exactly what you are trying to do as that is barely
PHP
code and makes little sense. Have you heard of http://www.php.net?
Here is a best guess to delete files modified more than 60 seconds that
foreach(glob('/temp/sess/*') as $file) {
if(time() - filemtime($file) > 60) {
$old = 'hadi';
$content = file_get_contents($file);
if(strpos($content, $old) !== false) {
unlink($file);
}
}
}
-Shawn
Post by hadi
Hi ,
Can someone help me to fix my code. It throwing error. I couldn't
determine how to do the for loop.
Here is my code
<?php
for ($i; in /tmp/sess*)
{
if (time()- filemtime($ip) > 60)
{
$old = 'hadi';
$filename = '$i';
$file = file_get_contents($filename);
strpos($file, $old);
unlink ($filename);
break();
}
}
?>
Loading...