Jump to content

File upload


pranshu82202

Recommended Posts

I have a file upload script which is working fine but i want to change the name of the uploaded file to a session varible $_session['id']... WHat should i do...

 

I tried to do it by writing the following the following code....

 

$_FILES["file"]["name"]=$_SESSION['id'];

 

But by this i looses the file extension...

 

File can be of nay type... (.pdf, .jpg, .gif, .png)

 

[email protected]

Link to comment
https://forums.phpfreaks.com/topic/246025-file-upload/
Share on other sites

// FILE uploading code

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {


   // $_FILES["file"]["name"]=$_SESSION['id'];


    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

$_SESSION['result']="SUCCESSFULLY UPDATED YOUR PROFILE";
header('location:members.php');
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}

Link to comment
https://forums.phpfreaks.com/topic/246025-file-upload/#findComment-1263549
Share on other sites

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
	function getExtension($str) 
{ 
		$i = strrpos($str,"."); 
        if (!$i) { return ""; }        
		$l = strlen($str) - $i;         

		 $ext ='.';  
		 $ext .= substr($str,$i+1,$l);      
		return $ext; 
}        

 

This code for session ID

		   $filename = stripslashes($_FILES['file']['name']);
	   //get the extension of the file in a lower case format 
	   $extension = getExtension($filename);
	   $extension = strtolower($extension); 

	   $New_filename = $_SESSION['id'];
	   $New_filename .=$extension;

	   echo "<br/><br/>".$New_filename ."<br/><br/>";

 

Link to comment
https://forums.phpfreaks.com/topic/246025-file-upload/#findComment-1263677
Share on other sites

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.