Jump to content

extracting the extension


jmr3460

Recommended Posts

I have got this code that I did that gets the extension from an uploaded file. I used

$_FILES['uploadedfile'] ['type'] and exploded the string to get my extension. My question: Is there a more reliable way to do this? This is my code:

	if (!empty($_FILES['uploadedfile']['tmp_name']))
{
	$file = $_FILES['uploadedfile']['tmp_name'];
	$old_file = basename($_FILES['uploadedfile']['tmp_name']);

	//Trying to get ext here
	$find_ext = explode('.', $old_file);
	//$ext = $find_ext[1];//line 16
	$find_type = $_FILES['uploadedfile']['type'];
	$get_ext = explode('/', $find_type);
	$ext = $get_ext[1];

	//This connects to server and select database
	//This section get information from db table for rename
	//START GET RENAME
	$db_name = "crowleys_minutes";
	$connection = mysql_connect("localhost", "user", "pw")or trigger_error(mysql_error());
    $db = mysql_select_db($db_name, $connection) or die(mysql_error());
    $sql = "SELECT * FROM minutes WHERE inserted = 0 LIMIT 1";
    $query = mysql_query($sql) or trigger_error(mysql_error());
    $result = mysql_fetch_array($query);
    $minute_id = $result['minute_id'];
    $id = sprintf('%03d',$minute_id);
    $year_folder = $result['year'];
    $area_date = $result['area_date'];
    $newfilename =  $id . "-crascmin" . $area_date . "." .$ext;//need to add $ext here
    $target_folder = "/home/crowleys/public_html/minutes/docs/" . $year_folder . "/";
    $target = $target_folder . $newfilename;
    //END GET RENAME
    //Here is where I am having trouble
	$upload = move_uploaded_file($file, $target);
		if ($upload){
			echo "Wa Hoo";
    }
    else
    {
        echo "There was an error uploading the file, please try again!";
    }
  }

I still have a little clean up to do. I think there are some unused variables that need to be removed.

Thanks for any help or suggestions.

Link to comment
Share on other sites

I guess there is not a problem solved anymore?

The modification, I believe, has yet to be re-added since the forum software upgrade. In the mean time, folks will just have to be old school and take a look at the posts to see if a thread has been solved. :shy:

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.