davelearning Posted January 10, 2011 Share Posted January 10, 2011 Hi Guys, I am having some issues in trying to return a file extension in a php upload script, here is what I am trying to do, no extension is added when a file is uploaded. $p_id = $row['p_id']; $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; $filename = $HTTP_POST_FILES['Filedata']['name']; $ext = end(explode('.', $filename)); $nfilename = $p_id . $ext; $targetFile = str_replace('//','/',$targetPath) . $nfilename; Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 10, 2011 Share Posted January 10, 2011 Try using this function: pathinfo($fileName, PATHINFO_EXTENSION) It will return the extention of the file. Quote Link to comment Share on other sites More sharing options...
davelearning Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks for your reply, however $p_id = $row['p_id']; $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; $filename = $HTTP_POST_FILES['Filedata']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION); $nfilename = $p_id . $ext; $targetFile = str_replace('//','/',$targetPath) . $nfilename; Gives the same result as before Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 10, 2011 Share Posted January 10, 2011 http://php.net/manual/en/function.pathinfo.php Quote Link to comment Share on other sites More sharing options...
davelearning Posted January 10, 2011 Author Share Posted January 10, 2011 Actually, sorry that did work, didnt refresh my folder d'oh! However it now misses the . between the file name and extension, how do I put this in? Many Thanks EDIT : also noticed that $HTTP_POST_FILES is deprecated, so changed to $_files Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2011 Share Posted January 10, 2011 $HTTP_POST_FILES has been deprecated since PHP 4.1.0. Use $_FILES. Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted January 10, 2011 Share Posted January 10, 2011 You can readd it: $nfilename = $p_id . "." . $ext; Quote Link to comment Share on other sites More sharing options...
davelearning Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks to everyone! Got it! $aquery = mysql_query("SELECT p_id FROM images WHERE p_id = '$l_id'"); while($row = mysql_fetch_assoc($aquery)) { $p_id = $row['p_id']; $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; $filename = $_FILES['Filedata']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION); $nfilename = '' . $p_id . '.' . $ext .''; $targetFile = str_replace('//','/',$targetPath) . $nfilename; } Quote Link to comment 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.