how to record the file extension of an uploaded file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

how to record the file extension of an uploaded file?

Post by deras »

how would i code things to pick up the file extension of an uploaded file? basically i am creating a random file name from the original name, which causes me to lose the original extension.

below is the part of the code where the upload and filename functions are ($r is the randomized name value). i am just giving all files .jpg extensions in the current code (until i figure out how ot get the correct extension)....

function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name=$r;
$yo='.jpg';
$file_name=$file_name.$yo;
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Something i used..

Code: Select all

function getextension($filename)
     {
    $filename 	= strtolower($filename);
	$extension 	= split("[/\\.]", $filename);
	$n 		= count($extension)-1;
	$extension 	= $extension[$n];
	return $extension;
    }

	$file_type 	= getextension($file_name);

   	if( $file_type!="gif" && $file_type!="jpg" && $file_type!="png" && $file_type!="doc"){
		echo 'extension wrong';
     die;
	}
Hope it is of some help
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}
?>
:roll:
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post by deras »

am i implementing it wrong....


----------------------------------------------------------------
function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];



function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}

$file_name=$r;
$yo=$ex;
$file_name=$file_name.$yo;

------------------------------------------------------------


is yielding a filename without an extension, i.e. no $ex value
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

deras wrote:$yo=$ex;
use:
$yo = ext($filename);
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post by deras »

made the changes, and i still am getting nothing out of the file extension variable (outputs just the randomized name with no extension / $yo variable)...

-------------------------------------------------------------------------------


function do_upload($upload_dir, $upload_url) {

for($len=8,$r='';strlen($r)<$len;$r.=chr(!mt_rand(0,2)?mt_rand(48,57):(!mt_rand(0,1)?mt_rand(65,90):mt_rand(97,122))));

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];

function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}

$yo = ex($filename);
$file_name=$r;
$file_name=$file_name.$yo;

-------------------------------------------------------------------------------
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

change:
$yo = ex($filename);
to:
$yo = ext($filename);
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post by deras »

fixed. the $yo variable is still coming up empty.

-------------------------------------------------------------------------------


function do_upload($upload_dir, $upload_url) {

for($len=8,$r='';strlen($r)<$len;$r.=chr(!mt_rand(0,2)?mt_rand(48,57):(!mt_rand(0,1)?mt_rand(65,90):mt_rand(97,122))));

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];

function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}

$yo = ext($filename);
$file_name=$r;
$file_name=$file_name.$yo;

-------------------------------------------------------------------------------
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

you actually need to send the function the $file_name variable....
because $filename and $file_name are 2 different things
chage to this ...

Code: Select all

function do_upload($upload_dir, $upload_url) {

for($len=8,$r='';strlen($r)<$len;$r.=chr(!mt_rand(0,2)?mt_rand(48,57):(!mt_rand(0,1)?mt_rand(65,90):mt_rand(97,122))));

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];

function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}

$yo = ext($file_name);
$file_name=$r;
$file_name=$file_name.$yo;
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

change:
$yo = ext($filename);
to:
$yo = ext($file_name);
EDIT: I'm too late!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post by deras »

thanks PrObLeM, that fixed it (i should have spotted that)... everything is working perfect now. thanks for everyone's help.


thanks for the heads up feyd. i will starting using the php tags.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

thank you.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could also just get the mime type from the $_FILES array. That would tell you what type the file is, without bothering with the extension.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply