Jump to content

Automatic Link Creator with Folder and File Structure.


BrianGilmore

Recommended Posts

I am hoping someone can help me out with this.  I am a total newb so I would need a script example I could copy/paste.  

 

Goal: Use my unlimited hosting plan for backing up my 4TB NAS via FTP client but avoiding a network violation from my hosting provider.  The provider insists that all files uploaded must be used on a website by at least a web page

 

Plan: Create a private web page that is nothing but a massive list of file paths where each file path listed is a hyper link to the file it describes.  This page must automatically update every time a new file is uploaded to the directory on the server that contains all the files I wish to backup.  There could be a polling interval for doing this.  It does not necessarily need to remove links to files that have been deleted or moved, but that would be a nice feature.  It does not need to be pretty.  It could either display everything in a nice tree structure or it could just be on long list of:

"

\\pictures\friends\johndoe01.jpg

\\pictures\family\brother01.jpg

... etc.

"\

each line entry linking to the file path listed.

 

Also, I would need to put this behind something that requires userid and password.

 

Can anyone help me with this?

 

Thanks!

 

Brian

Link to comment
Share on other sites

I am a total newb so I would need a script example I could copy/paste.

I feel your pain and I sympathize, but this isn't a "write my script for me" site. It's a learning site. Which means you must make an effort to write your own script, and we will try and help you if you get stuck. You get what you pay for. If you want the "write my script for me" option, you'll have better luck if you offer to throw money at a freelancer. But to prove I'm not just being a douche, here is a tl;dr about how you should approach this, should you endeavor to attempt this yourself.

 

One cheap and easy way to do it is to enable directory listing for the folder you intend to put your stuff in. How you do this varies depending on what server it is (e.g. apache vs. IIS), but here is example with apache:

 

In your apache config file you should have something similar to this:

 

<Directory /home/mywebuser/public_html>
	Options Indexes 
</Directory>
If you see this and there's a - in front of Indexes, that means it's currently disabled. Remove the - sign. If you do not see something like that in there, add it in. /home/mywebuser/public_html should be the server path to your public web directory. If it's there and already looks like that, congrats, you have nothing to change on that count. Restart apache if changes have been made.

 

An easy way to see if this is already enabled or not, is to go to a directory on your site that does not have an index.html or index.php type file in it. If you just get a page that looks like the contents of the directory are listed, then congrats, this step is already done. But if it automatically serves up a specific page (even a generic page by your host, or automatically serves up some 404 page) then it is currently not enabled.

 

Alternatively, your host may not allow you access to this (you don't really get access to it unless you have a virtual private server or dedicated server). So instead, you can put it in the .htaccess file in the folder like this (notice it is not wrapped in directory tag here):

 

Options Indexes
This will cause your server to output a listing of the files in the directory when you navigate to the directory, and they will be linkified, so that if you click on one, it will navigate to the file itself, instead of default to serving up index.html or w/e.

 

Then to password protect it, basically you just need to add some more code to .htaccess and also create a .htpasswd file with a [username]:[encrypted password] entry in it. Here is an article that explains how to do this.

 

 

Alternatively, if you don't want to do all that, and wish to instead have a script that lists the contents, well that's fairly easy to make as well. You don't need anything that constantly polls to update the list. Instead, the script would just read the contents of the directory at the time the script is executed. Basically you'd use any number of built in php functions such as glob, opendir/readdir, or RecursiveDirectoryIterator and loop through the results returned from it, outputting a link, using the path/to/filename as the href and link text.

 

And as far as password protecting the script, you can do something as simple as wrapping the whole thing in a condition such as

 

if ($_GET['user']=='username']&&$_GET['pass']=='password') {
  // stuff to list contents
}
And then you'd go to http://www.yoursite.com/script.php?user=username&pass=password. This isn't the most secure thing in the world, but maybe it's secure enough for your needs. I'd recommend at least changing the user/pass parameters to something more obscure like 'q1=xxx' and 'q2=xxx' where q1 is your user name and q2 is your password.

 

Now, that will only protect the script itself. Anybody would be able to go directly to the files without authentication if it's in a public directory. So if you want to go this route, you'd have to put your files outside of the public directory structure and then make your script serve up the file when the link is clicked. How you do this kind of depends on what the files actually are. You could do something as simple as use file_get_contents and just echo it out and hopefully the browser will be intelligent enough to guess the content type and act appropriately. I better way would be to also use header to output correct content type, based on the file, which basically involves having a lookup table of file type vs. content type. And that list will basically be what kind of files you actually have. And since the files themselves aren't in public dir, someone can't go directly to them. They'd have to go the password protected script to have them served up.

Link to comment
Share on other sites

Well, that's pretty mean making me write my own code and all, but I went ahead and sort of did it.  It took me a while to figure at one point that I needed to put these thingies before and after the script that I copied.... er... wrote:

 

    <?php

    ?>

 

But I finally managed to get a script working after that little hurdle. Now I would just need some help how to get the script I have to search into subdirectories and post the same.

 

Here is what I have now:

 

    <?php

    $d = dir(".");

    echo "Path: " . $d->path . "\n";

    echo "<ul>";

    while (false !== ($entry = $d->read())) {

       echo "<li><a href='{$entry}'>{$entry}</a></li>";

    }

    echo "</ul>";

    $d->close();

    ?>

 

Any ideas?  Would the commands you mentioned above accomplish this?

 

Any input would be appreciated.  Thanks!
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.