Jump to content

Getting file extension on upload


davelearning

Recommended Posts

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

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

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

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;



	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.