Jump to content

Help with $readdir script I made


Ninya

Recommended Posts

Hey guys basically with a simple script that I made I need some help with it's doing my head in.

 

What i'm trying to do is allow multiple filetypes to be shown by readdir, i want both .zip and .rar to be in the list, not just .zip how would i add .rar in this method as well to be visible on readdir?

 

<?php
$dirname = "/home/test/";
$dir = opendir($dirname);
$extension = array_pop(explode(".", $file));

?>

<?php
while(false != ($file = readdir($dir)))
{
if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "zip") {
echo("





<b>» <a href='$dirname$file'>$file - Download</a> <br />     </b>



");
}
}

?>

Link to comment
Share on other sites

  • 4 weeks later...

I've encountered a new problem,

 

now when i click on the link I get a 404 error because it's trying to link to the file within /home/public_html/filename.zip for example

 

How would I get it to just link from that directory itself?

 

So if it is at www.test.com/test/

 

When they click it how do i make it www.test.com/test/test1.zip as the hyperlink instead of www.test.com/home/public_html/test/test1.zip

 

How do i make it a proper link?

 

Thanks for the help.

Link to comment
Share on other sites

Huge apologies for triple post, but if anyone knew how I could add filter for only .zip and .rar to show in the below script it would be great as I got the below one working with the way i want links to work.

 

<?php

$dir="/xxx/xxx/xxx/xxx/"; // Directory where files are stored

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}

?>

Link to comment
Share on other sites

I don't want to sound useless and incompetent and I thank you so much for your reply and I am completely new to this - while I can read most PHP code I still have trouble writing it and would highly appreciate if you could finish the code for me, it is to play a huge role and aspect in a site I am creating.

 

I would be forever grateful and really appreciate everyone's help.

 

Regards, and sorry.

Link to comment
Share on other sites

I have written the code for you. Check pathinfo. The second parameter you can set an option. Let's look at the options available to us:

 

PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION and PATHINFO_FILENAME

 

Okay, all look pretty good. There is one there that stands out from the rest with what we are trying to do. PATHINFO_EXTENSION

 

You see, in the array $valid you specify the extensions that you want to show for download. But to check if the file we have found is valid, we need to get its extension. Test it out.

 

<?php

$dir="/xxx/xxx/xxx/xxx/"; // Directory where files are stored

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
// Run a test, let's echo out the files extension.
echo "PATHINFO EXTENSION IS: " . pathinfo($filename, PATHINFO_EXTENSION) . "<br />";
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}

?>

 

That's just your code, but it echos the extension of the file. See what I'm getting at.

 

Now, take a look at in_array.

 

in_array — Checks if a value exists in an array

 

We use in_array to check if our file extension is in the $valid array. in_array will return true if it finds the extension, or false if it can't find the extension.

Link to comment
Share on other sites

I got the ball rolling with this code.

 

<?php

$dir="/home/osmarkoo/public_html/test/"; // Directory where files are stored
$valid = array("zip", "rar");

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
if(in_array(pathinfo($filename, PATHINFO_EXTENSION), $valid)){

echo "<b>» <a href='/test/$filename'>" . basename($filename) . " - Download</a>     </b>";

}
?>

</a>   <br />



<?php
}
closedir($dir_list);
}

?>

 

It seems to work fine, do you notice any flaws? Thanks by the way.

Link to comment
Share on other sites

Just clean it up a bit. You have a random closing tag for the link tag. Indent your code to make it more readable and you should have something that looks a lot nicer.

 

<?php
$dir = "/home/osmarkoo/public_html/test/"; // Directory where files are stored
$valid = array("zip", "rar");

if ($dir_list = opendir($dir))
{
	while(($filename = readdir($dir_list)) !== false)
	{
		if(in_array(pathinfo($filename, PATHINFO_EXTENSION), $valid))
		{
			echo "<b>» <a href='/test/$filename'>" . basename($filename) . " - Download</a></b><br />";

		}
	}
closedir($dir_list);
}
?>

Link to comment
Share on other sites

No worries, and for reference, what you've done can also be achieved by what ken posted earlier. :P

 

<?php
$files = glob($dirname . '/*.{zip,rar}',GLOB_BRACE);
foreach ($files as $file) {
	echo "<b>» <a href='/files/" . basename($file) . "'>" . basename($file) . " - Download</a> <br />     </b>";
}
?>

 

It's good to know how it can work though. :)

 

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.