Jump to content

Find Filename less Extension


doubledee

Recommended Posts

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

 

 

Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

// 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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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%'..

Link to comment
Share on other sites

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 

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.