doubledee Posted June 8, 2012 Share Posted June 8, 2012 Sorry I'm so full of questions tonight?! My Upload Photo script generates a random Filename for the uploaded Photo, and produces something like this... f21190299a795e9cf3439f7f62c223f79e023ab7 What I need to do, is check to see if this new Filename exists, but the problem is that in my database I would have something like this... f21190299a795e9cf3439f7f62c223f79e023ab7.jpg So my question is... How do compare the new Filename against what is in the database field LESS the File Extension (e.g. ".jpg")?? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/ Share on other sites More sharing options...
l0gic Posted June 8, 2012 Share Posted June 8, 2012 // Not safe if filenames have a '.' in them $file = explode(".", "f21190299a795e9cf3439f7f62c223f79e023ab7.jpg"); $filename = $file[0]; // Returns: f21190299a795e9cf3439f7f62c223f79e023ab7 $fileext= $file[1]; // Returns: jpg Or you could use strlen and substr. There are many ways really. Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352067 Share on other sites More sharing options...
doubledee Posted June 8, 2012 Author Share Posted June 8, 2012 // Not safe if filenames have a '.' in them $file = explode(".", "f21190299a795e9cf3439f7f62c223f79e023ab7.jpg"); $filename = $file[0]; // Returns: f21190299a795e9cf3439f7f62c223f79e023ab7 $fileext= $file[1]; // Returns: jpg Or you could use strlen and substr. There are many ways really. Questions: 1.) Would sha1() ever return a period?? 2.) I think I need to approach this from a database standpoint. The full Photo Name exists in the database, so don't I need to grab the field "photo_name" less the File Extension at the end and then compare it to my Filename only generated in PHP? 3.) Is there a way to stay, "Start all the way to the right, and work backwards to the left until you find the first period." ?? (That would prevent issues if there were multiple periods in the filename, although, it all depends what sha1() returns...) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352069 Share on other sites More sharing options...
l0gic Posted June 8, 2012 Share Posted June 8, 2012 1.) Would sha1() ever return a period?? I don't believe so. I haven't used SHA1() much at all but from what I understand it returns an alphanumeric-only string. 2.) I think I need to approach this from a database standpoint. The full Photo Name exists in the database, so don't I need to grab the field "photo_name" less the File Extension at the end and then compare it to my Filename only generated in PHP? Is that not what you're doing already? Post the few lines of code that generate your filename, and the DB fieldnames you're using, etc.. I'll do a quick mock-up. Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352071 Share on other sites More sharing options...
doubledee Posted June 8, 2012 Author Share Posted June 8, 2012 l0gic, I was wrong and it looks like I have an $imageType earlier in my code. But my brain is not working tonight because I am really tired, so let me table this conversation for now as to not waste anymore of your time. I think I can figure this out tomorrow on a clean mind?! Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352072 Share on other sites More sharing options...
l0gic Posted June 8, 2012 Share Posted June 8, 2012 Are you accepting only JPG files? You could just add ".jpg" on newBasename..? Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352073 Share on other sites More sharing options...
l0gic Posted June 8, 2012 Share Posted June 8, 2012 Or... Change your SQL to use LIKE.. SELECT * FROM images WHERE image_name like '$newBasename%'; Which will look for anything that matches $newBasename followed by anything. So.. 'f21190299a795e9cf3439f7f62c223f79e023ab7%' would match 'f21190299a795e9cf3439f7f62c223f79e023ab7.jpg' but so would 'f21190299%'.. Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352075 Share on other sites More sharing options...
doubledee Posted June 8, 2012 Author Share Posted June 8, 2012 Are you accepting only JPG files? You could just add ".jpg" on newBasename..? No, it could be .JPG, .GIF, .PNG Okay, off to bed! Thanks, and I'll post more tomorrow morning. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352076 Share on other sites More sharing options...
l0gic Posted June 8, 2012 Share Posted June 8, 2012 Well if you have $imageType set to the format/extension uploaded you can just do something like: $match_test = $newBasename . "." . $imageType; echo $match_test; //should show you: filename.ext Quote Link to comment https://forums.phpfreaks.com/topic/263844-find-filename-less-extension/#findComment-1352078 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.