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)

 

-pranshu.a.11@gmail.com

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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