script to check contents of folder

bigjoe4

New Member
Messages
907
Reaction score
0
Points
0
I am making a website that will have a image upload feature and the files will be checked by an administrator before being dispalyed, but I need an easy way for the administrator to check if any files have been uploaded.

The files will be uploaded to a folder so I need a webpage that will tell me whether the folder contains images or not. Or maybe to get sent an email when a file is uploaded.

Can anyone help me with this?
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
The script makes the following assumption: the new files will be uploaded to a separate folder then the one from where they will be displayed.
Code:
<?php

$dir = ENTER DIRECTORY OF FOLDER HERE;
$extensions = ENTER WHICH FILE EXTENSIONS YOU WANT TO CHECK FOR; 



if(scandir($dir))
{
    $files = scandir($dir);

        for($i = 0; $i<count($files); $i++)
        {
              for($x = 0; $x<count($extensions); $x++)
               {
                  if(stristr($files[$i], $extensions[$x]))
                   {
                    if(strcmp(strtolower(stristr($files[$i], $extensions[$x])),      strtolower($extensions[$x]))==0)
                           echo "New File: ".$files[$i]."<br>";
                    }
             }
     }
 }
?>

The way this works: it takes all the files in your directory and compares them to the valid extensions you specify. If the extension is found it makes sure it is not someone messing with the server (i.e. going .jpg.exe) by making sure there is NOTHING after the extension type.
 
Last edited:

konekt

New Member
Messages
100
Reaction score
0
Points
0
Thanks, 100 credits sent
Edit:
it does not seem to work: http://circuitz4u.com/pcleds/upload.php
maybe it is because I need to upgrade my version of PHP?
Here are few things I would check:
-The Directory Path
-How did you declare your extensions?
-Are there any files with the valid extension in the folder you are looking at?

Here is the script I tested:
www.konekt.x10hosting.com/test.php

Here is how I defined my script:

Code:
<?php
[B]$dir = '/home/konekt/public_html'; 
$extensions = Array(".php", ".js");



[U]if[/U](scandir($dir))
{
     $files = scandir($dir);
     
     [U]for[/U]($i = 0; $i[B]<count($files); $i++)
     {
           [U]for[/U]($x = 0; $x<count($extensions); $x++)
           {
                  [U]if[/U](stristr($files[$i], $extensions[$x]))
                  {
                   [U]if[/U](strcmp(strtolower(stristr($files[$i], $extensions[$x])), strtolower($extensions[$x]))==0)
                         [U]echo[/U] "New File: ".$files[$i]."<br>[/B]";
                  }
          }
	}
}

?>

I don't have any images so I have it outputting all the php and javascript files.

Are you declaring your extensions with the period (i.e. .gif opposed to gif)? If not, that will cause it to find nothing.
[/B]
 

bigjoe4

New Member
Messages
907
Reaction score
0
Points
0
OK, i've tried to fix it but now it does this: http://circuitz4u.com/pcleds/upload.php

here is the code I used:
Code:
<?php

$dir = '/home/public_html/pcleds/upload/';
$extensions = Array(".jpg", ".pjpg", ".gif"); 



if(scandir($dir))
{
    $files = scandir($dir);

        for($i = 0; $i<count($files); $i++)
        {
              for($x = 0; $x<count($extensions); $x++)
               {
                  if(stristr($files[$i], $extensions[$x]))
                   {
                    if(strcmp(strtolower(stristr($files[$i], $extensions[$x])),      strtolower($extensions[$x]))==0)
                           echo "New File: ".$files[$i]."<br>";
                    }
             }
     }
 }
?>
 
Last edited:

konekt

New Member
Messages
100
Reaction score
0
Points
0
OK, i've tried to fix it but now it does this: http://circuitz4u.com/pcleds/upload.php

here is the code I used:
Code:
<?php

$dir = '/home/public_html/pcleds/upload/';
$extensions = Array(".jpg", ".pjpg", ".gif"); 



if(scandir($dir))
{
    $files = scandir($dir);

        for($i = 0; $i<count($files); $i++)
        {
              for($x = 0; $x<count($extensions); $x++)
               {
                  if(stristr($files[$i], $extensions[$x]))
                   {
                    if(strcmp(strtolower(stristr($files[$i], $extensions[$x])),      strtolower($extensions[$x]))==0)
                           echo "New File: ".$files[$i]."<br>";
                    }
             }
     }
 }
?>
I was able to replicate that error by pointing to a folder that did not exist. I was also able to replicate it by setting the permission to 0300. You need at least a 700 permission for it to read the directory properly.

So, things to do:
-Check that the upload directory actually exists and is directly in the pcleds folder.
-Make sure the upload directory permission is at least 700.
 

bigjoe4

New Member
Messages
907
Reaction score
0
Points
0
the directory definately exists and is in the right place! The upload directory permission is 755.
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
the directory definately exists and is in the right place! The upload directory permission is 755.
When we click on the error, we get the following: The requested URL /pcleds/function.scandir was not found on this server.

This tells us that the function, scandir, does not exist. The ability to look through the content of directories came in PHP5, if the function can not be found it means that the proper version of php is not installed on your server. The function I used came with php5, so I re-wrote the method to only use PHP4 methods:

Code:
<?php
[B]
$dir = 'ADD YOUR FOLDER''; 
$extensions = Array("ADD YOUR EXTENSIONS");;



[U]if[/U]($files = opendir($dir))
{
    [U]while[/U]([U]false[/U] != ($file=readdir($files)))
{ 
	
		[U]for[/U]($x=0;$x[B]<count($extensions); $x++)
		{
                  [U]if[/U](stristr($file, $extensions[$x]))
                  {
                   [U]if[/U](strcmp(strtolower(stristr($file, $extensions[$x])), strtolower($extensions[$x]))==0)
                         [U]echo[/U] "New File: ".$file."<br>[/B]";
                  }
		}
          
	
}
}
?>
[/B]
Same rules apply:
add the dots on the extensions
make sure the directory is valid.
 
Top