Jump to content

Archived

This topic is now archived and is closed to further replies.

ttomnmo

Downloading files

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Think you have an extra ( there

try this:
if (strlen($file) > 2)

instead of
if ((strLen($file) > 2)

Share this post


Link to post
Share on other sites
I put on my glasses and got passed another parsing error, but I ran into a

Call to a member function on a non-object

error 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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites

×

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.