Re: file validation

From: Date: Fri, 01 Feb 2002 22:32:38 +0000
Subject: Re: file validation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-83208@lists.php.net to get a copy of this message
On Fri, 2002-02-01 at 14:02, toni baker wrote: > I would like to prevent users from uploading a file > that contains more than 4 pipes or less than 2 pipes. > The code below prevents users from uploading a file > containing more than 4 pipes, but not less than 2 > pipes. Should I use awk, ereg, or sed? Thanks Unless you *have* to spawn processes to do this for you for some reason, you can keep things a lot simpler by doing it in PHP, something like this: <?php error_reporting(E_ALL); function check_file($filename) { if (!$upload_file = file($filename)) { return false; } while (list(, $line) = each($upload_file)) { $num_pipes = substr_count($line, '|'); if ($num_pipes < 2 || $num_pipes > 4) { return false; } } return true; } $userfile = 'testpipes.txt'; if (check_file($userfile)) { echo "File passed.\n"; } else { echo "File failed.\n"; } ?> Hope this helps, Torben > system ("/bin/cat $userfile|/bin/sed -n > 's/.*|.*|.*|.*|.*|/&/p'> pipes.txt; > $fd =fopen("pipes.txt", 'r'); > $pipes5=fgets($fd,50); > echo ($pipes5); > fclose($fd); > > if ($pipes5) { > print "wrong number of pipes"; > } > > The uploaded file below should not pass but it does: > > aaaaa|bbbbb|ccccc|ddddd|eeeee > aaaaa|bbbbb|ccccc| > aaaaa|bbbbb|ccccc|ddddd > aaaaa|bbbbb > aaaaa|bbbbb|ccccc|ddddd|eeeee -- Torben Wilson <torben@php.net> http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506

« previous php.general (#83208) next »