Jump to content

Recommended Posts

Hey.

 

I am new to php.

 

Anyways basically i am working on a website and its seems to be going rather well. Although as of late i have been thinking deeply over things. Basically i am stuck on a downloads page. I am great with HTML but very new to php. So i know that with HTML if say for example i wanted to display an image of a downloadable file and i wanted to make that image link to the file no problem. But here is the issue. If i am uploading files through ftp i would have to manually edit the HTML every time to display the new files.

 

I have seen a fair few scripts around on Google etc. Basically i am wondering if it is possible to scan a folders files with php, then display the files. I found scripts for this. But the problem is i would like to be able to have a script the scans the folder, displays images and program links in order. So if a person wanted to see what a files looked like they can see a image screenshot and then click the file link and download the file. It would be really good if i can display an items image automatically through php. This will save me time and i was curious if it is possible? Maybe even if i had to display an image directory and then the file directory although that would probably be pointless if i can give them both the same file name so they are displayed in order?

 

Anyways could anyone please help me at all or point me in the right direction.

Link to comment
https://forums.phpfreaks.com/topic/264869-need-some-help-if-possible-please/
Share on other sites

How are the image screenshots being uploaded?  What type of files are the others being downloaded?  If you prepend the word "IMAGE" or something similar, you'd be able to easily discern which ones are the image files.  Alternatively you could just look at the last 3 chars of the filename and look for JPG, GIF, PNG, etc.  Alternatively you can look for the period for the file extension and grab everything after that to also catch the random jpeg extension. 

It's fairly simple to scan a directory and list the files in there and display them.  I did something similar almost 10 years ago when I first started working with PHP.  What is it you're trying to do that the scripts you've looked at aren't showing you how to accomplish?  It's just a matter of:

Get a list of the files in the directory

foreach and build your links as you go (OL/UL maybe?  Table? What's more appropriate to the overall site design?)

 

Sorry for the late reply our power has been off all day.

 

I have a display script which works good. it scans a directory and displays all the files, then name, filetype and size and date modified in a table.

 

But the problem is instead of displaying an image instead it just give a link to download images.

Sorry for the double post.

 

I cannot find the edit button:/

 

Anyways i didnt post the php script i am using. NOTE: i did not make this it is from the web and i have altered it the maker gave permission to alter it and use it as long as i do not claim it as my own work.

 

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>test</title>
   <style>
     *{
         padding:0;
         margin:0;
     }
     html,body {
	 color:#333;
         font-family: "Lucida Console", Courier, monospace;
         font-size:14px;
         text-shadow:1px 1px 1px #cacaca;
         -webkit-text-shadow:1px 1px 1px #cacaca;
         -moz-text-shadow:1px 1px 1px #cacaca;
	 background-image:url('../images/background.png');
     }
     a{
         padding: 2px 0 0 24px;
         color:#2F2F2F;
         text-decoration:none;
     }
     a:hover{
         color:#000;
     }
     #container{
         margin:0 auto;
         width:700px;
         margin-top:20px;
         padding-top:10px;
         border:1px solid #EEE;
         border-radius:10px;
         -moz-border-radius:10px;
     }
     .head{
         background-color:#00BFFF;
         color:#FFF;
         font-weight:bold;
         padding:7px 0 5px 10px;
         font-size:14px;
         letter-spacing:1px;
         font-family: Verdana, Arial, Helvetica, sans-serif;
     }
     .head:hover{background-color:#6C7B8B;}
     .head span{font-size:9px; letter-spacing:0;}
     td{
         background-color:#D4E2F0;
         padding:6px;
     }
     td:hover{background-color:#6C7B8B;}
     h1{
         font-size:18px;
         font-weight:bold;
         padding:0 0 10px 10px;
     }

/*icons for file types (add more to suit your needs.)*/	
	a[href$=".rar"] {background: url(../images/rar.png) no-repeat left 50%;}
	a[href$=".zip"] {background: url(../images/rar.png) no-repeat left 50%;}

   </style>
</head>
<body>
   <div id="container">
       <?php
         $directory = "../files/";  // for index use($directory = "./";
	 // opens this directory
	 $myDirectory = opendir($directory);


// Lets select a directory to open
$myDirectory = opendir($directory);



// Lets get each entry
while($entryName = readdir($myDirectory)) {
           $dirArray[] = $entryName;
         }

// Lets find the extentions of files
         function findexts ($filename)
         {
           $filename = strtolower($filename) ;
           $exts = split("[/\\.]", $filename) ;
	   //$exts = explode("[/\\.]", $filename) ;
           $n = count($exts)-1;
           $exts = $exts[$n];
           return $exts;
         }

// Lets make it close the directory
         closedir($myDirectory);

// Lets count elements in array
         $indexCount   = count($dirArray);

// Lets sorts the files
        sort($dirArray);

// Lets Display the files
         print("<h1>test</h1>");
         print("<table width='100%' cellspacing='10'>
                 <tr>
                   <td class='head'>Filename</td>
                   <td class='head'>Type</td>
                   <td class='head'>Size <span>(bytes)</span></td></tr>\n");

// Lets loop through the array of files and display them all
         for($index=0; $index < $indexCount; $index++) {
               if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
               print("<tr><td><a href='$directory$dirArray[$index]'>$dirArray[$index]</a></td>");
               print("<td>");
               print(findexts($dirArray[$index]));
               print("</td>");
               print("<td>");
               print(filesize($directory.$dirArray[$index]));
               print("</td>");
               print("</tr>\n");
           }
         }
         print("</table>\n");
       ?>

</body>
</html>

 

If i could find out how to add to that, if it finds an image(.png will be used only) it will display it instead of linking them to it that would be awesome...

  • 2 weeks later...
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.