Ninya Posted July 13, 2010 Share Posted July 13, 2010 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> "); } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 I would use the glob function: <?php $files = glob($dirname . '/*.{zip,rar}',GLOB_BRACE); foreach ($files as $file) { echo "<b>» <a href='$file'>" . basename($file) . " - Download</a> <br /> </b>"; } ?> Ken Quote Link to comment Share on other sites More sharing options...
Ninya Posted July 13, 2010 Author Share Posted July 13, 2010 Thanks Ken, I was trying to utilize glob too but couldn't quite grasp it, I've got much to learn. Thanks I'll try it when I get home. Quote Link to comment Share on other sites More sharing options...
Ninya Posted August 6, 2010 Author Share Posted August 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
Ninya Posted August 6, 2010 Author Share Posted August 6, 2010 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); } ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 Create an array with valid file extensions: $valid = array("zip", "rar"); Then use in_array and pathinfo. if(in_array(pathinfo($filename, CHECK_THE_MANUAL), $valid)){ Now you have to check the manual. Quote Link to comment Share on other sites More sharing options...
Ninya Posted August 6, 2010 Author Share Posted August 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
Ninya Posted August 6, 2010 Author Share Posted August 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 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); } ?> Quote Link to comment Share on other sites More sharing options...
Ninya Posted August 6, 2010 Author Share Posted August 6, 2010 Yeah, just did that my self after I noticed it, messy playground! haha. Thanks for all the help I really appreciate it Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 No worries, and for reference, what you've done can also be achieved by what ken posted earlier. <?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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.