how to record the file extension of an uploaded file?
Moderator: General Moderators
how to record the file extension of an uploaded file?
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;
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;
Something i used..
Hope it is of some help
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;
}Code: Select all
<?php
function ext($filename)
{
$ex = explode(".", $filename);
return $ex[count($ex)-1];
}
?>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
----------------------------------------------------------------
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
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;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
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;
-------------------------------------------------------------------------------
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;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
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;
-------------------------------------------------------------------------------
you actually need to send the function the $file_name variable....
because $filename and $file_name are 2 different things
chage to this ...
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
deras, read Posting Code in the Forums