Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,987 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,380 people online right now. Registration is fast and FREE... Join Now!




easy problem, check file extensions

2 Pages V  1 2 >  
Reply to this topicStart new topic

easy problem, check file extensions

nickfzx
14 Nov, 2006 - 01:09 PM
Post #1

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
Hi, I am not a programmer and this is my first post.

I run a illustration community website here:
http://www.amateurillustrator.com/

I need to run a check for jpg, gif or png files...but in a slightly unusualy way.

here is the code i need to add the check to:

CODE
        if($cpg_udb->can_join()){
            if ($CONFIG['enable_avatar']){
                ($row['avatar_url'] != "") ? $avatar_url= "<img src='".AVATAR_PATH.$row["avatar_url"].".gif' class='image' style='float: left; margin-right: 5px;' alt='' />" : $avatar_url = '';
                    
            }


if you look right now it says .gif in there...so what is happening is that it is only find files that are gif's, and I want it to be able to check weather is has found a gif and if it hasn't then it looks for a jpg and then a png.

I tried this:
CODE
        if($cpg_udb->can_join()){
            if ($CONFIG['enable_avatar']){
                ($row['avatar_url'] != "") ? $avatar_url= "<img src='".AVATAR_PATH.$row["avatar_url"].".gif' class='image' style='float: left; margin-right: 5px;' alt='' />" : $avatar_url = '';
                
                ($row['avatar_url'] != "") ? $avatar_url= "<img src='".AVATAR_PATH.$row["avatar_url"].".png' class='image' style='float: left; margin-right: 5px;' alt='' />" : $avatar_url = '';
                
                ($row['avatar_url'] != "") ? $avatar_url= "<img src='".AVATAR_PATH.$row["avatar_url"].".jpg' class='image' style='float: left; margin-right: 5px;' alt='' />" : $avatar_url = '';
                
                
            }


but that just uses the last repetition and returns only jpg images.

How could I set this up.

If you are interested I am wanting to retrieve avatars from a folder...I have a few users and they have been uploading avatars in these 3 formats.



Help on this would be much appreciated.

All the best

Nick

This post has been edited by nickfzx: 14 Nov, 2006 - 01:10 PM
User is offlineProfile CardPM
+Quote Post

Tuzoid
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 02:35 PM
Post #2

D.I.C Head
Group Icon

Joined: 20 Aug, 2006
Posts: 120



Thanked: 2 times
Dream Kudos: 50
My Contributions
I'm not exactly what your asking for...but you could try:
$_FILES['file_post_name']['type'] as a variable

This returns the header type, some examples:
JPG : image/jpg
PNG : image/png
GIF : image/gif

I think the GD library has some options too, not too sure on that one though confused.gif
User is offlineProfile CardPM
+Quote Post

nickfzx
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 02:47 PM
Post #3

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
basically the code i have at the moment gets the filename without the extension.

is there a code for retreaving the file extension if you know the filename.

so say the file is called dog.jpg, the code at the moment gives just dog. is their a way of finding out the .jpg bit(or whatever it is) from the knowledge of just filename. It will only be a gif, jpg or png.

Is this a bit clearer?

Thanks for your help

This post has been edited by nickfzx: 14 Nov, 2006 - 02:48 PM
User is offlineProfile CardPM
+Quote Post

Tuzoid
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 04:42 PM
Post #4

D.I.C Head
Group Icon

Joined: 20 Aug, 2006
Posts: 120



Thanked: 2 times
Dream Kudos: 50
My Contributions
Ahh, I see what you mean...I'll make a quick code for you smile.gif

CODE

<?php
/* File Name Variable */
$filename = 'thisisatest.jpg';
/* New File Name */
$newname = substr( $filename , -3 );

echo $newname;
?>


Here is an example of this script:
http://tuz.hopto.org/dic/user-help/file-extension.php
User is offlineProfile CardPM
+Quote Post

nickfzx
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 04:52 PM
Post #5

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
sorry i must have been unclear again

What I want is the filename with the extension but I don't know what the extension is...

what I have is the filename with no extension...and I know the extension is either going to be gif, jpg or png.

The code gets the filename (without the extension) from a sql database. It then looks in the avatars folder for files with the name it gets from the database...but it doesn't find anything because it doesn't have the file extension.

So i need to change the code that I have so that it will try and find the file by adding a jpg extension and if it doesn't find one then it tries .gif and then .png.

Sorry if I havent been clear and thanks a lot for your help

This post has been edited by nickfzx: 14 Nov, 2006 - 04:53 PM
User is offlineProfile CardPM
+Quote Post

grimpirate
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 04:54 PM
Post #6

D.I.C Head
Group Icon

Joined: 3 Aug, 2006
Posts: 226



Thanked: 6 times
Dream Kudos: 375
My Contributions
This snippet can actually be found at php.net in the FileSystem Functions section
CODE
<?php
$fn = 'whatever.jpg';
$fext = substr($fn, strrpos($fn, '.') + 1);
?>

If you want to include the dot then just leave out that + 1.

EDIT:If you're reading the file from a database and not getting its extension then the problem lies with the operations you're using to retrieve the filename. I don't know much about MySQL, but for instance if you use the scandir() function that gets all file names and directory names from a location along with their extensions.

This post has been edited by grimpirate: 14 Nov, 2006 - 04:58 PM
User is offlineProfile CardPM
+Quote Post

nickfzx
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 05:06 PM
Post #7

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
QUOTE(grimpirate @ 14 Nov, 2006 - 05:54 PM) *

This snippet can actually be found at php.net in the FileSystem Functions section
CODE
<?php
$fn = 'whatever.jpg';
$fext = substr($fn, strrpos($fn, '.') + 1);
?>

If you want to include the dot then just leave out that + 1.

EDIT:If you're reading the file from a database and not getting its extension then the problem lies with the operations you're using to retrieve the filename. I don't know much about MySQL, but for instance if you use the scandir() function that gets all file names and directory names from a location along with their extensions.

Here is the nub of the matter...I am not using an operation to retrieve the filename...I am using an operation to retrieve a users id.

Then if a user has uploaded an avatar that avatar will be called users_id.jpg or .gif or .png and will be in the avatars folder.

So i want to take that users_id I get from the database and scan the avatars folder for users_id.jpg and if it doesn't find it then it tries users_id.png then it tries users_id.gif.


thanks for the help.

This post has been edited by nickfzx: 14 Nov, 2006 - 05:07 PM
User is offlineProfile CardPM
+Quote Post

Tuzoid
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 05:12 PM
Post #8

D.I.C Head
Group Icon

Joined: 20 Aug, 2006
Posts: 120



Thanked: 2 times
Dream Kudos: 50
My Contributions
I'll go ahead and make this snippet, and I'll post it in the code snippets...I'll edit this post once I get it done smile.gif
User is offlineProfile CardPM
+Quote Post

Tuzoid
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 05:44 PM
Post #9

D.I.C Head
Group Icon

Joined: 20 Aug, 2006
Posts: 120



Thanked: 2 times
Dream Kudos: 50
My Contributions
Ok, I think I got what you want this time:
CODE

<?php
//==========
// Configuration
//==========
$directory = '/var/www/'; //trailing slash
$wtlf = 'signature'; //what to look for

//======
// Snippet
//======
$flag = false;
$ext = array( '.jpg' , '.gif' , '.png' );
for( $i = 0; count( $ext ) > $i; $i++ )
{
    if( file_exists( $directory . $wtlf . $ext[$i] ) )
    {
        $flag = true;
    }
}

if( $flag == true )
{
    echo 'file exists!';
}
?>


Edit the directory & wtlf variables. $wtlf should be what ever the user's id is. Hopefully this helped
User is offlineProfile CardPM
+Quote Post

nickfzx
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 05:48 PM
Post #10

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
cool thanks a lot for your time...I'll try now to integrate that with my original code snippet and then post the result when i'm done.

Thanks a lot
User is offlineProfile CardPM
+Quote Post

nickfzx
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 05:56 PM
Post #11

New D.I.C Head
*

Joined: 14 Nov, 2006
Posts: 37


My Contributions
oh one last thing, your code echo's "file exhists" at the end.

would it be possible to change this so that it echo's the name of the file...

eg the original $wtlf + whatever it finds the extension to be (jpg gif or png)

I couldn't see how to do this.

This post has been edited by nickfzx: 14 Nov, 2006 - 05:57 PM
User is offlineProfile CardPM
+Quote Post

Tuzoid
RE: Easy Problem, Check File Extensions
14 Nov, 2006 - 07:10 PM
Post #12

D.I.C Head
Group Icon

Joined: 20 Aug, 2006
Posts: 120



Thanked: 2 times
Dream Kudos: 50
My Contributions
Sure, here is a modified version of my code smile.gif

CODE

<?php
//==========
// Configuration
//==========
$directory = '/var/www/'; //trailing slash
$wtlf = 'signature'; //what to look for

//======
// Snippet
//======
$flag = false;
$ext = array( '.jpg' , '.gif' , '.png' );
for( $i = 0; count( $ext ) > $i; $i++ )
{
    if( file_exists( $directory . $wtlf . $ext[$i] ) )
    {
        $flag = true;
        $name = $wtlf . $ext[$i];
    }
}

if( $flag == true )
{
    echo $name;
}
?>

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month