Quickest way to retrieve filenames

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Quickest way to retrieve filenames

Post by maqmus »

I have a file where I store data like this:

Code: Select all

<?php
<img src="somefile.gif" width="35" heigth="65">\n
<a href="somepage.html" target="_blank">My link</a>\n
<object.....(the whole code for flash files) ="myflashfile.swf" ...object>
?>
Now I'll delete a line, but I want to delete the used file (somefile.gif or myflashfile.swf). So I need to retrieve the filename from the array.
I'll be using only 4 sufixes: .html, .gif, .jpg and .swf.

What should be the quickest way to do that?

P.S. Before the "n" should be a backslash, but I can't get it to print it out.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try this:

Code: Select all

preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches);
// $matches contains the .. matches  
print_r($matches);
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

I tried this but I get an Array and I can't read anything from it.
This is what I did:

Code: Select all

<?php
preg_match("#="?((/?[a-z0-9_-]+/?)+\.(gif|jpg|swf))"?#i",$line[1],$matches); 
print_r($matches);

foreach($matches as $esto) {
echo $esto;
}
?>
This way I get nothing.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$line = '<img src="somefile.gif" width="35" heigth="65">';

preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches); 
print_r($matches);
outputs

Code: Select all

Array
(
    &#1111;0] =&gt; ="somefile.gif"
    &#1111;1] =&gt; somefile.gif
    &#1111;2] =&gt; somefile
    &#1111;3] =&gt; gif
)
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

When I try with the text line (as your example) it works.

But geting the data from the file it doesn't. I added echo (the source) and I get the image (I have a .gif file as $file[0])

Code: Select all

<?php
$data = "alterbanner/docs/lista.php";
$file = file($data);
array_shift($file);
preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$file[0],$matches); 
print_r($matches[1]);
echo "<br><br>".$file[0];
?>
This way I can see the image but not the filename.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$file = array(
'<?php',
'<img src="somefile.gif" width="35" heigth="65">\n',
'<a href="somepage.html" target="_blank">My link</a>\n',
'<object.....(the whole code for flash files) ="myflashfile.swf" ...object>',
'?>'
);

foreach($file as $line)
{
	if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches))
	{
		echo $matches[1]."\n";
	}
}
outputs

Code: Select all

somefile.gif
somepage.html
myflashfile.swf
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

I can't understand it.

When trying as you did, it works. But when I read from the file (lista.php) with the same content I get a blank page.
:?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dunno.. works perfectly fine as an file() call for me..
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

I tried everything and nothing works.

When I use

Code: Select all

<?php
$file = file("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches))
{ 
echo $matches[1]."<br>"; 
} 
} 
?>
I get a blank page (delayed). And when I echo the $line I get the gif image, flash file, etc.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php 
$file = array("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches)) 
{ 
echo $matches[1]."<br>"; 
} 
} 
?>
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

It doesn't find the matches in the file.
I tried this:

Code: Select all

<?php
$file = array("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches)) 
{ 
echo $matches[1]."<br>Hello_one"; 
} 
echo "Hello_two";
} 

?>
And I get only Hello_two on my page, like if there weren't any matches.
Post Reply