ttomnmo Posted October 6, 2006 Share Posted October 6, 2006 I gratefully recieved some help a few days ago on a file upload page. I am now trying to write something that will show files that have been uploaded to a directory and allow them to be viewed or downloaded. A friend helped me with this <? $mydir = 'upload_dir/'; while(($file = $mydir->read()) !== false) { //echo "Filename: $file<BR>"; if ((strLen($file) > 2) echo "<tr><td>Filename: <a href= 'upload_dir/$file' target=_blank>$file</a></td></tr>"; } $mydir->close(); print("</table>");?><h3>Please click on file name to download or view.</h3></div><div id="content"><?} else {?><div id="column_left"><?}?>and I am getting a parsing error right around here if ((strLen($file) > 2) I am having trouble finding info on what I am trying to do.Can anyone help or point me towards help? Quote Link to comment Share on other sites More sharing options...
yonta Posted October 6, 2006 Share Posted October 6, 2006 Think you have an extra ( theretry this: if (strlen($file) > 2)instead of if ((strLen($file) > 2) Quote Link to comment Share on other sites More sharing options...
ttomnmo Posted October 6, 2006 Author Share Posted October 6, 2006 ThanksMaybe i need glasses! Quote Link to comment Share on other sites More sharing options...
ttomnmo Posted October 6, 2006 Author Share Posted October 6, 2006 I put on my glasses and got passed another parsing error, but I ran into a Call to a member function on a non-objecterror with while(($file = $mydir->read()) !== false) { I am trying to have the files in the upload_dir shown on the page with the ability to download them, how can I get the directory files displayed on the page? Quote Link to comment Share on other sites More sharing options...
yonta Posted October 7, 2006 Share Posted October 7, 2006 The error is telling you that there should have a php object named my dir ($mydir) and it should have a method read() ($mydir->read() ) but you don't. Somewhere in your script you should have $mydir = new MyClass();where myclass is the name of the class that handles reading directories. If you can't find it, you'll need a function to read a directory (i can help you there). Either that or ask your friend who first helped you.Best Quote Link to comment Share on other sites More sharing options...
ttomnmo Posted October 8, 2006 Author Share Posted October 8, 2006 I really appriciate all your help. I'm trying to do this as best I can without hastleing you guys cause your time is important. I was looking around and found an older code for displaying the files<? $file_dir="/upload_dir"; $dir=opendir($file_dir); while ($file=readdir($dir)) { if ($file != "." && $file != "..") { echo "<a href=".$file_dir."/".$file." target=_blank>".$file."</a>"; echo "<br>"; } } ?> and I get the files to display as links, but when I try to view them I get a window saying page cannot be found or save target as I get IE unable to download. Am I better off trying to go back to what I had before and setting up a read directory function or continueing with this new thing here?Thanks for your help, I need it! Quote Link to comment Share on other sites More sharing options...
shiny_spoon Posted October 8, 2006 Share Posted October 8, 2006 I tried your (slightly modified) code with rawurlencode() and it worked fine for all sorts of filetypes and filenames.[code=php:0]<?php $file_dir="upload_dir"; $dir=opendir($file_dir); while ($file=readdir($dir)) { if ($file != "." && $file != "..") { echo "<a href=".$file_dir."/".rawurlencode($file)." target=_blank>".$file."</a><br/>"; } }?> [/code]If you have anymore questions about this project, feel free to send me a PM. I coded a huge file browser a few months back for a company using similar code. :) 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.