Jump to content

Need to add to this php script.


Westin

Recommended Posts

Hello,

I need to add a bit of code to this php script.

 

It's a very simple script that takes an arguement as file=NameofFile

and returns the actual file/image to the browser screen.

 

Here is the small script:

<?php

    $file = $_GET['file'];
    $fileDir = '/path/to/secured/documents/';

    if (file_exists($fileDir . $file))
    {
        // Note: You should probably do some more checks 
        // on the filetype, size, etc.
        $contents = file_get_contents($fileDir . $file);

        // Note: You should probably implement some kind 
        // of check on filetype
        header('Content-type: image/jpeg');

        echo $contents;
    }

?>

 

Now, the problem is this:

 

Somebody initially enters form data to our database as a name of such:

john_BeLushi

 

and there is an image but it's name is John Belushi

 

So the script never finds the image because the case isn't the same as what the person initially entered into the database.

 

What I need this script to do, is to ignore the case sensitivity of the name of the file that resides on the server and just bring back the file like it normally does regardless if someone has typed in john belushi or JOHN BELUSHI or JoHn bEluSHi

 

Can anybody give me a quick and clean addition to this script so that it ignores the case sensitivity of the name of the file stored on the server please.

Link to comment
Share on other sites

Problem is there are some docs/images stored as John Smith of America  or John Smith the Brothers Smith  or still, John Smith in England or John Smith of the River in England

 

Generally, theres only a handful of words that are never capitalized (of, the, in on )  Theres more than that, but not too many more.

 

 

 

Link to comment
Share on other sites

SELECT * FROM users WHERE name = 'john belushi'
SELECT * FROM users WHERE name = 'JOHN BELUSHI'
SELECT * FROM users WHERE name = 'JoHn bEluSHi'

 

will always return the same result unless name has the attribute BINARY set. However it will not find John_Belushi use therefor the operator LIKE:

 

SELECT * FROM users WHERE name LIKE 'john_belushi'

 

Notice that _ here is a character pattern and will also match a space or any other as long as it is only exactly one character long

 

more information can be found on the mysql documentation http://dev.mysql.com/doc/refman/6.0/en/string-comparison-functions.html#operator_like

Link to comment
Share on other sites

Then what you could do is make it an array:

 

<?php

$higherletters = array("Of", "The", "In");
$lowerletters = array("of", "the", "in");
$file = ucwords(strtolower($_GET['file']));

$file = str_replace($higherletters, $lowerletters, $file);

?>

 

Im pretty sure that works. Just add the words needed.

Link to comment
Share on other sites

Okiay, I think I know how to do with this.  I believe my ftp program has the ability to change all files to lower case when it uploads them, so I'll re-upload the 169,253 files to the server and then use your the way mentioned to make it look for lowercase all the time.

 

Thanks,

Link to comment
Share on other sites

So, if I were to put that all together into one script it would look like this?

<?php
    $higherletters = array("Of", "The", "In");
    $lowerletters = array("of", "the", "in");
    $file = ucwords(strtolower($_GET['file']));

$file = str_replace($higherletters, $lowerletters, $file);
    
    $fileDir = '/path/to/secured/documents/';

    if (file_exists($fileDir . $file))
    {
        // Note: You should probably do some more checks 
        // on the filetype, size, etc.
        $contents = file_get_contents($fileDir . $file);

        // Note: You should probably implement some kind 
        // of check on filetype
        header('Content-type: image/jpeg');

        echo $contents;
    }

?>

Link to comment
Share on other sites

Perfect.  Thank you.  It'll take me much less time to quickly scan through all the files seeing if there is any other oddball lower case words all the time that I might have to add, but as it is this seems to work out great.

 

Much appreciated.

Link to comment
Share on other sites

Woops, I ran into a snaggletooth.

 

For some reason the following file won't be found with the new script: Burrenton Forge-Tender

 

But if I call it up with the old script with the right case sensitivity, the file is found. Maybe the - are going to produce a problem?

 

Yah, indeed the - in any document names are a problem, all of them can't be found when they have a - in them.

 

And then theres problems when a name as In as the first 2 letters of the name or On for that matter etc.  Such as Incinerate or Onmi Giants Fan  Then the files won't be found either.

 

I think theres too many variable possibilities with this maybe.

Link to comment
Share on other sites

I'm not quite sure how to overcome this, as it would require some more complex coding.

 

This looks like it may help, but I am not sure:

 

<?php

function mb_ucwords($str) {
    $exceptions = array();
    $exceptions['Hp'] = 'HP';
    $exceptions['Ibm'] = 'IBM';
    $exceptions['Gb'] = 'GB';
    $exceptions['Mb'] = 'MB';
    $exceptions['Cd'] = 'CD';
    $exceptions['Dvd'] = 'DVD';
    $exceptions['Usb'] = 'USB';
    $exceptions['Mm'] = 'mm';
    $exceptions['Cm'] = 'cm';
    //    etc.
    
    $separator = array(" ","-","+");
    
    $str = mb_strtolower(trim($str));
    foreach($separator as $s){
        $word = explode($s, $str);

        $return = "";
        foreach ($word as $val){
            $return .= $s . mb_strtoupper($val{0}) . mb_substr($val,1,mb_strlen($val)-1);
        }
        $str = mb_substr($return, 1);
    }

    foreach($exceptions as $find=>$replace){
        if (mb_strpos($return, $find) !== false){
            $return = str_replace($find, $replace, $return);
        }
    }
    return mb_substr($return, 1);
}

?>

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.