Results 1 to 9 of 9

Thread: Check if file is JPG

  1. #1

    Check if file is JPG

    Hi

    I am creating a simple script that uses scandir() to generate a XML with the images list inside a folder. My doce is:

    PHP Code:
    $dir = "images/"; //You could add a $_GET to change the directory 
    $files = scandir($dir); 
    echo "<?xml version='1.0'?>";
    echo "<list>";
    foreach($files as $key => $value){ 
    echo "<image>".$value."</image>"; //You could add an icon in here maybe, a link to the file/directory, or a link to list files in that directory 
    echo "</list>";

    ?>
    The only problem, or lets call bug, I found in this script is that if I have some other file with another extension the script will list it too.

    What I need is a way to check if the value is a jpg. Can anyone give me a hint?

    Thanks

  2. #2
    1,627
    posts
    hugeExplosions = true;
    Quote Originally Posted by ruiganga View Post
    Hi

    I am creating a simple script that uses scandir() to generate a XML with the images list inside a folder. My doce is:

    PHP Code:
    $dir = "images/"; //You could add a $_GET to change the directory 
    $files = scandir($dir); 
    echo "<?xml version='1.0'?>";
    echo "<list>";
    foreach($files as $key => $value){ 
    echo "<image>".$value."</image>"; //You could add an icon in here maybe, a link to the file/directory, or a link to list files in that directory 
    echo "</list>";

    ?>
    The only problem, or lets call bug, I found in this script is that if I have some other file with another extension the script will list it too.

    What I need is a way to check if the value is a jpg. Can anyone give me a hint?

    Thanks
    Hey dude, I'd delve a little deeper than just copy/paste from another script if you want to be able to solve problems like this yourself.

    Check out the PHP manual at www.php.net

    The solution is simple

    PHP Code:
    $dir = "images/"; //You could add a $_GET to change the directory 
    $files = scandir($dir); 
    echo "<?xml version='1.0'?>";
    echo "<list>";
    foreach($files as $key => $value){ 
    if(strpos($value, ".jpg") > 0) {
    echo "<image>".$value."</image>"; //You could add an icon in here maybe, a link to the file/directory, or a link to list files in that directory 
    }
    echo "</list>";

    ?>
    http://us2.php.net/manual/en/function.strpos.php
    Last edited by Charleh; August 28th, 2009 at 11:10 AM.
    MS Paint FTW!


  3. #3
    Hi

    Thanks for the help. It was not a simple copy/paste and I've search the PHP library looking for the solution. I was just asking what should be the best method. The solution I've found myself so far was:

    <?
    $dir = "images/"; //You could add a $_GET to change the directory
    $files = scandir($dir);
    $file_type = "jpg";

    // start writing the xml header
    echo "<?xml version='1.0'?>";
    echo "<list>";
    // list all the files checking each if end with the correct file extension
    foreach($files as $key => $value){
    if(stristr($value, $file_type) === FALSE) {
    // do nothing

    } else {
    // add file to XML
    echo "<file>".$value."</file>";
    }

    // ends the XML
    echo "</list>";
    ?>

    Thanks anyway.

  4. #4
    jwilliam's Avatar
    476
    posts
    An intellectual carrot!
    Don't hold me to this, but I believe the strpos function that Charleh mentioned is the most efficient way to match substrings in php. Plus, I'm not a fan of having empty conditional blocks, like the section where you have "// do nothing" -- at least, when it's not necessary.

  5. #5
    Hi

    Thanks to all who helped. This is my final code:

    <?
    /*
    file: listDir.php

    created by: RuiGanga - SymbolBox.pt

    This code was created to generate a XML file with a list of specific file type
    of the files inside a directory.

    To use this script just replace the file type and directory name in the URL:

    (...URL...)/listDir.php?dir="images"&type=".jpg"

    if none is defined it will search for images directory and .jpg files

    */

    // main variables
    $dir = "images/";
    $file_type = ".jpg";

    // check $_GET["dir"]
    if($_GET["dir"] == ""){
    $dir = "images/";
    }else{
    $dir = $_GET["dir"]."/";
    }

    // check $_GET["type"]
    if($_GET["type"] == ""){
    $file_type = ".jpg";
    }else{
    $file_type = $_GET["type"];
    }

    // check if directory exists
    if (file_exists($dir)) {
    // THIS DIRECTORY EXISTS
    // start searching files inside the directoru
    $files = scandir($dir);
    /* checks if there is some files inside the directory.
    For default, even if directory is empty, it resolves the number 2
    (that are the ".." and "..." kind of file) thats why I use 2 instead of ZERO*/
    if(count($files) <= 2){
    // IF NO FILES SHOWS A XML FILE THAT SAYS IT
    echo "<?xml version='1.0'?>"."\n";
    echo "<!-- THE DIRECTORY IS EMPTY -->"."\n";
    echo "<list>"."\n";
    echo "<nofiles> NO FILES </nofiles>"."\n";
    echo "</list>"."\n";
    }else{
    // WE HAVE FILES, LETS GENERATE A XML WITH THE LIST
    $totalFiles = count($files);
    //subtract the 2 kind of files that exists even if the direcotry is empty
    $totalFiles = $totalFiles-2;
    echo "<?xml version='1.0'?>"."\n";
    echo "<!-- ## WE HAVE ".$totalFiles." FILES ## -->"."\n";
    echo "<!-- ## INSIDE ".$dir." DIRECTORY ## -->"."\n";
    echo "<list>"."\n";
    // USING FOREACH TO DO THE LIST
    foreach($files as $key => $value){
    // checks if the file is from the correct extension ($file_type)
    if(stristr($value, $file_type) === FALSE) {
    echo "<!-- ".$value." is not a ".$file_type." file -->"."\n";
    }else{
    echo "<file>".$value."</file>"."\n";
    }
    }
    // ends the XML
    echo "</list>"."\n";
    }
    } else {
    // IF THE DIRECTORY DOES NOT EXISTS
    echo "<?xml version='1.0'?>"."\n";
    echo "<!-- THE DIRECTORY DOES NOT EXIST -->"."\n";
    echo "<list>"."\n";
    echo "<nodirectory> NO DIRECTORY </nodirectory>"."\n";
    echo "</list>"."\n";

    }
    ?>

    I've decided to keep stristr() function because this way it is NOT case-sensitive, so the image.JPG file and image.jpg file will appear in the XML.

    Thanks once again. I hope this code can help anyone.

  6. #6
    jwilliam's Avatar
    476
    posts
    An intellectual carrot!
    This script seems unsafe to me. You're basically allowing users to peek at the contents of any directory on your box that your web server has access to. I'd think about some way of locking this script down better.

    As a side note... there is another version of strpos called stripos, which is case-insensitive. (php 5 and later only) If you're using php4 you can use this:

    PHP Code:

    if (!function_exists("stripos")) {
      function 
    stripos($str,$needle,$offset=0)
      {
          return 
    strpos(strtolower($str),strtolower($needle),$offset);
      }


  7. #7
    Hi

    i want the confirmation of file ectension not to be case sensitive.

    Thank for the comment about security. I guess i'll make a new get atribute as a password and the script will only rum if the password ínside the php is the same

    what do you think?

  8. #8
    jwilliam's Avatar
    476
    posts
    An intellectual carrot!
    That's definitely a step in the right direction

  9. #9
    All this substr stuff is bogus. Anyone can have a file disguised as a JPG by just changing the extension. You better look at the actual contents of the file with exif_imagetype: http://www.php.net/manual/en/functio...-imagetype.php .

    If your PHP doesn't have that installed then you can define it yourself:

    PHP Code:
    // ===========================================================================
    // exif_imagetype
    //    args:  string - path to image file
    //    ret:   constant telling you what file type it is
    //    about: couldn't figure out how to install it on my WAMP, so I defined it
    //           here.
    // ---------------------------------------------------------------------------
    if(!function_exists('exif_imagetype'))
    {
            function 
    exif_imagetype ($filename)
            {
                    if((list(
    $width$height$type$attr) = getimagesize($filename)) !== false)
                            return 
    $type;
                    else
                            return 
    false;
            }


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012