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; Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/ 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. Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157518 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 Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157520 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 Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157522 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 Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157526 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. Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157528 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; Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157536 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; } Link to comment https://forums.phpfreaks.com/topic/223994-getting-file-extension-on-upload/#findComment-1157540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.