Page 1 of 1
how to record the file extension of an uploaded file?
Posted: Mon Jun 21, 2004 2:22 am
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;
Posted: Mon Jun 21, 2004 2:27 am
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
Posted: Mon Jun 21, 2004 4:02 am
by qads
Code: Select all
<?php
function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}
?>

Posted: Mon Jun 21, 2004 4:54 am
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
Posted: Mon Jun 21, 2004 5:36 am
by qads
deras wrote:$yo=$ex;
use:
$yo = ext($filename);
Posted: Mon Jun 21, 2004 12:28 pm
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;
-------------------------------------------------------------------------------
Posted: Mon Jun 21, 2004 1:46 pm
by PrObLeM
change:
$yo = ex($filename);
to:
$yo = ext($filename);
Posted: Mon Jun 21, 2004 2:26 pm
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;
-------------------------------------------------------------------------------
Posted: Mon Jun 21, 2004 2:30 pm
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;
Posted: Mon Jun 21, 2004 2:31 pm
by Illusionist
change:
$yo = ext($filename);
to:
$yo = ext($file_name);
EDIT: I'm too late!
Posted: Mon Jun 21, 2004 2:56 pm
by feyd
Posted: Mon Jun 21, 2004 3:20 pm
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.
Posted: Mon Jun 21, 2004 3:39 pm
by feyd
thank you.
Posted: Mon Jun 21, 2004 4:17 pm
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.