jmr3460 Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/ Share on other sites More sharing options...
mrMarcus Posted November 22, 2009 Share Posted November 22, 2009 pathinfo() <?php $ext = pathinfo ($_FILES['uploadedfile']['name']); $ext = $ext['extension']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963367 Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 Or, to save a temporary variable, pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION) See http://php.net/pathinfo for details. Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963369 Share on other sites More sharing options...
jmr3460 Posted November 22, 2009 Author Share Posted November 22, 2009 This only brings the p from the pdf. $get_ext = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION); $ext = $get_ext['extension']; Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963376 Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 You don't need that second line. Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963378 Share on other sites More sharing options...
jmr3460 Posted November 22, 2009 Author Share Posted November 22, 2009 Thanks for the help. That did it. I guess there is not a problem solved anymore? Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963379 Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182522-extracting-the-extension/#findComment-963401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.