Jump to content

Downloading files


ttomnmo

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?
Link to comment
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?
Link to comment
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
Link to comment
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!
Link to comment
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. :)
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.