Jump to content

File Type Function


Talon21

Recommended Posts

Hi all,

 

I'm new to PHP, at the moment I'm working on a script that will scan my Movies folder for example and will create a table with the necessary information. such as a link to IMDB etc...

I want to add a new <td> with the File Type information. here is the code:

 

<html>
<head>
<style>
.imdb{
font-family: verdana;
font-size:14px;
}
.imdb td{
border: 1px solid #EFEFEF;
}
.imdb th	{
border: 1px solid #EFEFEF;
}
</style>
</head>
<center>
<body>
<table class="imdb">
<tr>
<th>Movie</th>
<th>IMDB</th>
<td>
<?php

//scaning the movies folder

$dir    = 'G:\Home\Movies';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
for ($a = 0 ;$a < count($files1); $a++)

//if conditinon to delete the "." and ".." folders from the table

if ($files1[$a] == ".")
echo "";
else if ($files1[$a] == "..")
echo "";
else	
//echo the movie table
echo '<tr><td>'."$files1[$a]".'</td>
      <td><a href="http://www.imdb.com/find?q='."$files1[$a]".'&sourceid=mozilla-search">IMDB</a></td></tr>';


?>		  
</tr>
</td>
</table>
</body>
</center>
</html> 

Link to comment
Share on other sites

Use pathinfo():

 

<html>
<head>
<style>
.imdb{



font-family: verdana;



font-size:14px;
}
.imdb td{



border: 1px solid #EFEFEF;
}
.imdb th



{



border: 1px solid #EFEFEF;
}
</style>
</head>
<center>
<body>
<table class="imdb">
<tr>
<th>Movie</th>
<th>IMDB</th>
<td>
<?php

//scaning the movies folder

$dir    = 'G:\Home\Movies';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
for ($a = 0 ;$a < count($files1); $a++)

//if conditinon to delete the "." and ".." folders from the table

if ($files1[$a] == ".")



echo "";
else if ($files1[$a] == "..")



echo "";
else



$info = pathinfo($files1[$a]);
//echo the movie table



echo '<tr><td>'."$files1[$a]".'</td>



      <td><a href="http://www.imdb.com/find?q='."$files1[$a]".'&sourceid=mozilla-search">IMDB</a></td>



      <td>Extension: '.$info['extension'].'</td></tr>';






?>





  
</tr>
</td>
</table>
</body>
</center>
</html> 

 

Edit: Don't know why the layout between the [ code ] tags got so messed up.

Link to comment
Share on other sites

Just found you had some errors in your table construction. You really should indent your code too. I cleaned it up a bit, try this:

 

<html>
<head>
<style type="text/css">
	.imdb{
	font-family: verdana;
	font-size:14px;
	}
	.imdb td{
	border: 1px solid #EFEFEF;
	}
	.imdb th{
	border: 1px solid #EFEFEF;
	}
</style>
</head>
<body>
<center>
<table class="imdb">
	<tr>
		<th>Movie</th>
		<th>IMDB</th>
		<th>Extension</th>
	</tr><?php
//scaning the movies folder
$dir    = 'G:\Home\Movies';
$files1 = scandir($dir);

for ($a = 0 ;$a < count($files1); $a++) {
//if conditinon to delete the "." and ".." folders from the table
if ($files1[$a] == "." || $files1[$a] == "..") {continue;}

//load file info into $info
$info = pathinfo($files1[$a]);

//echo the movie table
echo '
	<tr>
		<td>'.$files1[$a].'</td>
		<td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td>
		<td>Extension: '.$info['extension'].'</td>
	</tr>';
}
?>
</table>
</center>
</body>
</html>

Link to comment
Share on other sites

Else you can use explode to get the extension:

 

<html>
<head>
<style type="text/css">
	.imdb{
	font-family: verdana;
	font-size:14px;
	}
	.imdb td{
	border: 1px solid #EFEFEF;
	}
	.imdb th{
	border: 1px solid #EFEFEF;
	}
</style>
</head>
<body>
<center>
<table class="imdb">
	<tr>
		<th>Movie</th>
		<th>IMDB</th>
		<th>Extension</th>
	</tr><?php
//scaning the movies folder
$dir    = 'G:\Home\Movies';
$files1 = scandir($dir);

for ($a = 0 ;$a < count($files1); $a++) {
//if conditinon to delete the "." and ".." folders from the table
if ($files1[$a] == "." || $files1[$a] == "..") {continue;}

//split string at dots to get extension
$info = explode('.', $files1[$a]);

//echo the movie table
echo '
	<tr>
		<td>'.$files1[$a].'</td>
		<td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td>
		<td>Extension: '.end($info).'</td>
	</tr>';
}
?>
</table>
</center>
</body>
</html>

Link to comment
Share on other sites

Oh, I didn't know that. So the title you're echoing is from the directory I see. To get the extension, we then have to scan each directory separately. I guess each movie directory contains a bunch of files (movie + subtitles and such), so we have to distinguish the movie file from the rest of the files.

 

I would check if the current "file" is a directory (is_dir()); if yes then run another scandir(), then run through each file (foreach()) and set the extension if it's one of the following: avi, mpg, mpeg, mp4, mov, wmv (just tell me if you want help adding more types):

 

<?php
//get file extension of video file inside the movie directory
if (is_dir($dir.'\\'.$files1[$a])) {
	$files = scandir($dir.'\\'.$files1[$a]);
	foreach ($files as $file) {
		$info = explode('.', $file);
		$ext = strtolower(end($info));
		if (in_array($ext, array('avi', 'mpg', 'mpeg', 'mp4', 'mov', 'wmv'))) {
			$extension = $ext;
			break;
		}
	}
}
?>

 

Whole script:

 

<html>
<head>
<style type="text/css">
	.imdb{
	font-family: verdana;
	font-size:14px;
	}
	.imdb td{
	border: 1px solid #EFEFEF;
	}
	.imdb th{
	border: 1px solid #EFEFEF;
	}
</style>
</head>
<body>
<center>
<table class="imdb">
	<tr>
		<th>Movie</th>
		<th>IMDB</th>
		<th>Extension</th>
	</tr><?php
//scaning the movies folder
$dir    = 'G:\Home\Movies';
$files1 = scandir($dir);

for ($a = 0 ;$a < count($files1); $a++) {
//if conditinon to delete the "." and ".." folders from the table
if ($files1[$a] == "." || $files1[$a] == "..") {continue;}

//get file extension of video file inside the movie directory
if (is_dir($dir.'\\'.$files1[$a])) {
	$files = scandir($dir.'\\'.$files1[$a]);
	foreach ($files as $file) {
		$info = explode('.', $file);
		$ext = strtolower(end($info));
		if (in_array($ext, array('avi', 'mpg', 'mpeg', 'mp4', 'mov', 'wmv'))) {
			$extension = $ext;
			break;
		}
	}
}

//echo the movie table
echo '
	<tr>
		<td>'.$files1[$a].'</td>
		<td><a href="http://www.imdb.com/find?q='.urlencode($files1[$a]).'&sourceid=mozilla-search">IMDB</a></td>
		<td>Extension: '.$extension.'</td>
	</tr>';
}
?>
</table>
</center>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.