Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string comparison problems 1

Status
Not open for further replies.

skibascott

IS-IT--Management
Joined
Mar 18, 2004
Messages
82
Location
US
My goal here is to compare $phpdocnum (selected from a select list on the previous page)with the contents of $file_list. When executing the code here, my foreach loop finds no matches, even though I can see the value of $phpdocnum is exactly the same as a number of values in the $file_list(I have previously confirmed with an echo command the contents of $file_list). Funny thing is, when I manually declare the value of $phpdocnum to a value that I know is in $file_list, I get the desired results(a list of values in $file_list that match the string of $phpdocnum). Why is it that when I specify the value of $phpdocnum I get matches, but when I use the value I am getting from POST I do not get matches. I have confirmed that they hold the same value.

Code:
$list = "cd /mnt/sc_proc/video; ls *.wmv | sort"; 
exec($list,$file_list);    
	    
$phpdocnum = $_POST['PHPdocNum']; 
$phpdocnum = substr_replace($phpdocnum, '-', 4, 1);
$phpdocnum = substr_replace($phpdocnum, '', 5, 1);

echo $phpdocnum."<br>";

foreach ($file_list as $value){
       	if ($phpdocnum == substr($value, 0, 6)){
	echo $value."<br>";
	}
	}
 
Here's a different idea:

Instead of listing all the files with an exec statement why don't you just check for the existence of the file you are looking for?
Code:
if (file_exists('/mnt/sc_prov/video/'.$_POST['PHPdocNum']){
    echo("File ".$_POST['PHPdocNum']." exists.";
}

What does PHPdocNum contain? Anything that gets escaped by the system?
 
The contents of $phpdocnum for example: 4.00.09. I use the substr_replace function to convert $phpdocnum to the same format as the filenames in $file_list. An example of a file name in $file_list is: 4.00-9_4.11_blastextrudehead.wmv. This is why I use the substr function in the foreach loop. It seems to me(correct me if I am wrong) that the file_exists function would not work because of the all of the substr_replace and substr functions I am performing on the variable.
 
I understand.
If you have PHP 4.3.0 (at least) then you can get around the exec() which I would recommend.
Code:
<?php
foreach (glob("/*.wmv") as $filename) {
   # your comparison code here...
}
?>

Also, you could use the strstr function to find a match. Inspect the strings and see if they match in length - there may be invisible whitespace characters somwhere.
 
Please excuse my ignorance, this is my first time working with php. When using this glob function, where do I specify the directory to be searched?

I performed a strlen on my $phpdocnum(4.00-9) and the value is 8. So there must be some whitespace characters that is throwing off my comparison.
 
I am still not getting any matches. Can you see anything wrong with this code? Even when I change the comparsion to "not equal to" I get no matches. When I was using exec before, I would get the entire list of filenames in the directory when using "not equal to". Also, when I use the trim function, it doesn't trim any whitespace, the strlen returns the same value. Any ideas?

Code:
foreach (glob('mnt/sc_proc/video/*.wmv') as $filename) {
								if ($phpdocnum == substr($filename, 0, 6)){
   								        echo $filename."<br>";
								}
}
 
Your code looks fine.
Dump out all filenames to see if the glob function returns the desired set of files:
Code:
foreach (glob('mnt/sc_proc/video/*.wmv') as $filename) {
   echo("Found: ".$filename."<br>";
   if ($phpdocnum == substr($filename, 0, 6)){
      echo "This is it: ".$filename."<br>";
   }
}

The path you have in the example doesn't start at the root of the filesystem, it's relative to the location of the PHP script. If there's nothing in such a subfolder nothing will happen.
 
Nice Pointer DRJ148, a star.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I tried what you suggested and still am not getting any results. I have tried every combination of paths to the directory and even copied the directory to the same directory my script is in. I have confirmed that I have php version 4.2.3, so the glob function should work. The problem seems to be with the glob function or the path to the directory. When the directory to be searched is in the directory of my script the glob function should look like this, correct?

(glob('/video/*.wmv') as $filename)
 
1. All right, if I read your post correctly:
I have confirmed that I have php version 4.2.3, so the glob function should work.
The PHP manual says glob is available for PHP > 4.3.0, so it's not available if you really have 4.2.3 and an error should occur. There's a glob() replacement for the built in functions on the PHP site in the glob() documentation page.

2. All PHP file functions start at the file system root, not the web server document root. So your PHP file could be in a URL like but the system file path would look like /var/To glob() or use any file function I recommend to start at the root of the file system and give a full absolute path.
 
I really don't know since I don't know what the filesystem looks like. If your web servers' document root is /var/www/ then you are right.
Look at the output of phpinfo() and find the document root of the web server (unless you are sure that's it).
 
The document root of the webserver is /export/home0/apache/htdocs
Using this path also yields no filenames.



 
Path to the video files should be:
/export/home0/apache/htdocs/gredescnet/videos/

Did you write it like that?
 
I just tried that and still no filenames.

foreach (glob('/export/home0/apache/htdocs/gredescnet/video/*.wmv') as $filename)

(typo in an earlier post, videos directory is actually video)
 
Out of curiosity, what happens if you use:

glob('/export/home0/apache/htdocs/gredescnet/video/*.[blue]*[/blue]')

?




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Good idea, but same results.

Code:
<?php 
$phpdocnum = $_POST['PHPdocNum']; 
$phpdocnumtitle = $_POST['DocNum'];
			
$phpdocnum = substr_replace($phpdocnum, '-', 4, 1);
$phpdocnum = substr_replace($phpdocnum, '', 5, 1);
		
echo strlen($phpdocnum);
echo $phpdocnum;
foreach (glob('/export/home0/apache/htdocs/gredescnet/video/*.*') as $filename) {
   	echo "Found: ".$filename."<br>";
   	if ($phpdocnum == substr($filename, 0, 6)){
      	echo "This is it: ".$filename."<br>";
   											}
								}
?>

We are not missing anything in the path are we? The following worked before, as you can see in my first post.

$list = "cd /mnt/sc_proc/video; ls *.wmv | sort";
 
So, then, have you tried the /mnt/sc_proc/video/ path with glob()?
 
why don't u create a temporary .php file in the videos directory with the code:
Code:
<?

  echo realpath($PHP_SELF);

?>

that should give you the full path to that dir. then just delete the file and use the given path ;)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top