pranshu82202 Posted August 30, 2011 Share Posted August 30, 2011 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 More sharing options...
voip03 Posted August 30, 2011 Share Posted August 30, 2011 can you post your code Link to comment https://forums.phpfreaks.com/topic/246025-file-upload/#findComment-1263510 Share on other sites More sharing options...
pranshu82202 Posted August 30, 2011 Author Share Posted August 30, 2011 // 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 More sharing options...
voip03 Posted August 30, 2011 Share Posted August 30, 2011 //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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.