mikenichols mikenichols - 4 months ago 16
PHP Question

Dynamic link created based on new file upload?

Every 72 hours I upload a new PHP file to my server. (well actually it is an xml file transformed on the server with php) Is there a method to create a link on an html page that links to the "new" PHP doc automatically everytime a new file is uploaded?

I don't want to manually change the link every 72 hours. I would ultimately like to have an html page with a list of links to every new doc that is uploaded. I found this for images but I need someting like this but for PHP files and links.
http://net.tutsplus.com/articles/news/scanning-folders-with-php/

Any help would be very appreciated.




I found a solution that add links to the xml files. Now I just need to figure out how to add a link to reference the xslt sheet for each new xml file that is upload AUTOMATICALLY. I am not sure how to do this but any help would be very helpful. Thanks for everyones help.

<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("<a href=\"".$file."\">".$file."</a><br />\n");
}
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?>

JJ. JJ.
Answer

You can do that pretty easily with PHP function readdir:

http://php.net/manual/en/function.readdir.php

Simply loop through the files in the directory where you upload files and have php output a link for each.

ie:

<?php
if ($handle = opendir('/path/to/upload_dir')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo '<a href="http://myserverdir/' . $file . '">' . $file . '</a><br />';
        }
    }
    closedir($handle);
}
?>

You'll need to edit the http:// URL on the href to point to the correct download URL for your server, as well as the server path for opendir.

Hope that helps.