Westin Posted April 29, 2009 Share Posted April 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/ Share on other sites More sharing options...
mattal999 Posted April 29, 2009 Share Posted April 29, 2009 For always lowercase files: $file = strtolower($_GET['file']); For first letter capitalization (John Smith): $file = ucwords(strtolower($_GET['file'])); That should work. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822016 Share on other sites More sharing options...
Westin Posted April 29, 2009 Author Share Posted April 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822024 Share on other sites More sharing options...
ignace Posted April 29, 2009 Share Posted April 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822026 Share on other sites More sharing options...
mattal999 Posted April 29, 2009 Share Posted April 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822030 Share on other sites More sharing options...
Westin Posted April 29, 2009 Author Share Posted April 29, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822055 Share on other sites More sharing options...
Westin Posted April 29, 2009 Author Share Posted April 29, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822060 Share on other sites More sharing options...
mattal999 Posted April 29, 2009 Share Posted April 29, 2009 Yes. Try it. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822072 Share on other sites More sharing options...
Westin Posted April 29, 2009 Author Share Posted April 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822076 Share on other sites More sharing options...
Westin Posted April 29, 2009 Author Share Posted April 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822099 Share on other sites More sharing options...
mattal999 Posted April 29, 2009 Share Posted April 29, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156154-need-to-add-to-this-php-script/#findComment-822107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.