sean14592 Posted April 8, 2008 Share Posted April 8, 2008 Ok, Hi, My propblem is that I have a form which allows people to upload a image (gif,jpeg,jpg,bmp,png). the image can be catched using: $_FILES['photo1']['name'] I can upload the image and save the image name in my database, but the problem I have is I need to rename the image but keep the extention the same. So my question summed up is: How do I rename the image while keeping its extention and then save this to variable? Please can someone help, my I have tried searching, but was not successfull. Cheers Sean Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/ Share on other sites More sharing options...
Caesar Posted April 8, 2008 Share Posted April 8, 2008 <?php $ogname = $_FILES['photo1']['name']; $xplode = explode('.',$ogname); $nuname = 'caca'; $nufile = $nuname.$xplode[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-512141 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 This is a very simple example. $extension = strrchr($_FILES['photo1']['name'], "."); $newname = "newname"; $newimage = $newname.$extension; echo $newimage; Caesar your code works but what if the name is mypic.john.jpg Ray Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-512142 Share on other sites More sharing options...
Caesar Posted April 8, 2008 Share Posted April 8, 2008 Details, details! :-P Good point though. Was a crude example Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-512145 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 LOL I actually started off doing the same exact thing you did!! Ray Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-512167 Share on other sites More sharing options...
sean14592 Posted April 8, 2008 Author Share Posted April 8, 2008 Thanks people sooooo much, I will try it out and then mark it as solved if works, Im sure it will Cheers Sean Preston Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-512244 Share on other sites More sharing options...
sean14592 Posted April 11, 2008 Author Share Posted April 11, 2008 Hi, I have tried both, but did not manage to get it to work. my full code is: $user = $_SESSION['username']; ////INSERT ALL VARIABLES INTO DATABASE! $connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error()); mysql_select_db($database,$connect) or die("cannot select database!".mysql_error()); $check = mysql_query("select id from data where username='$user'") or die(mysql_error()); if (mysql_num_rows($check) > 0) { $a_record = mysql_fetch_array($check); $id = $a_record['id']; } //Creat image directory mkdir("../../pages/".$id."/images"); $upload_dir = '../../pages/'; // You will need to set this. $upload_file = $upload_dir . $id ."/images/" . basename($id . "_" . $_FILES['photo1']['name']); $photo1url = $id . "_" . $_FILES['photo1']['name']; //Get database cell - photo1url mysql_select_db(********) or die('Error, cannot select mysql database'); $query = "UPDATE properties SET photo1url = '$photo1url'". "WHERE id = '$id'"; mysql_query($query) or die('Error, query failed'); Thabove gets the image uploads it and saves the image name in the database, what I need to do is rename this. would somebody be able to rewrite this so it will do that. I would be very, very greatful. Cheers Sean Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-515019 Share on other sites More sharing options...
craygo Posted April 11, 2008 Share Posted April 11, 2008 What do you want to rename it to. It looks like you are renaming it already $upload_file = $upload_dir . $id ."/images/" . basename($id . "_" . $_FILES['photo1']['name']); id_filename.ext Not sure what you want I guess. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-515041 Share on other sites More sharing options...
sean14592 Posted April 11, 2008 Author Share Posted April 11, 2008 I would like to rename it to 1.ext ext = the original extention of the image uploaded. Cheers Sean Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-515062 Share on other sites More sharing options...
craygo Posted April 11, 2008 Share Posted April 11, 2008 by one I am guessing that would be the id of the current user. <?php $user = $_SESSION['username']; ////INSERT ALL VARIABLES INTO DATABASE! $connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error()); mysql_select_db($database,$connect) or die("cannot select database!".mysql_error()); $check = mysql_query("select id from data where username='$user'") or die(mysql_error()); if (mysql_num_rows($check) > 0) { $a_record = mysql_fetch_array($check); $id = $a_record['id']; } //Creat image directory mkdir("../../pages/".$id."/images"); $upload_dir = '../../pages/'; // You will need to set this. $ext = strrchr($_FILES['photo1']['name'], "."); $n_name = $id; // Set this to whatever you want the name to be $newname = $n_name.$ext; $upload_file = $upload_dir . $id ."/images/" . $new_name; move_uploaded_file($_FILES['photo1']['tmp_name'], $upload_file); // this will just insert the new name. You will have to point to the path when you output results from the db $photo1url = $newname; //Get database cell - photo1url mysql_select_db(********) or die('Error, cannot select mysql database'); $query = "UPDATE properties SET photo1url = '$photo1url'". "WHERE id = '$id'"; mysql_query($query) or die('Error, query failed'); ?> Ray Quote Link to comment https://forums.phpfreaks.com/topic/100165-solved-rename-image-while-keeping-extention/#findComment-515071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.