wmguk Posted November 25, 2008 Share Posted November 25, 2008 Hi, I have a script that uploads a file from a browse button to a specific folder on my server... if($cvsel=='yes'){ //UPLOAD FILE $target = "../cv/"; $target = $target . basename( $_FILES['cv']['name']) ; $filename = basename( $_FILES['cv']['name']); if(move_uploaded_file($_FILES['cv']['tmp_name'], $target)) { $msg = "The file ". basename( $_FILES['cv']['name']). " has been uploaded"; } else { $err = '1'; $msg = "There was an error uploading the file " . basename( $_FILES['cv']['name']) . " please try again!"; How can I rename the file? for example, after the info has been enterered in to the database I want to rename it $candid + . mysql_insert_id() . (A3261) (3261 is the auto insert id from the db and A is the first initial of his name... this is all my code (you'll see the file is uploaded before the information is in the database.... <?php //error_reporting(0); include ("connection.php"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db($db, $con); $title = $_POST['title']; $firstname = $_POST['firstname']; $surname = $_POST['surname']; $address = $_POST['address']; $dob = $_POST['dob']; $tel = $_POST['tel']; $mob = $_POST['mob']; $email = $_POST['email']; $prefercontact = $_POST['prefercontact']; $nationality = $_POST['nationality']; $legalent = substr($_POST['legalent'], 2); $english = substr($_POST['english'], 2, -1); $convict = substr($_POST['convict'], 0, -1); $convictiondetails = $_POST['convictiondetails']; $cvsel = $_POST['cvsel']; $cvattached = $_POST['cvsel']; $work1 = $_POST['work1']; $work2 = $_POST['work2']; $work3 = $_POST['work3']; $worktype = implode(",", $_POST['worktype']); $ftpt = implode(",", $_POST['ftpt']); $avoidwork = $_POST['avoidwork']; $avoidemployer = $_POST['avoidemployer']; $datesavailable = $_POST['datesavailable']; $qualifications = $_POST['qualifications']; $experience = $_POST['experience']; $licences = $_POST['licences']; $transport = $_POST['transport']; $comments = $_POST['comments']; $searchhow = $_POST['searchhow']; $signature = $_POST['signature']; $candid = substr($surname, 0, 1); $type = 'Candidate'; echo $cvsel; //CHECK FOR EMAIL ADDRESS ALREADY USED /* Select data from database. */ $sql="SELECT * FROM candidateinfo WHERE email='$email'"; $result=mysql_query($sql); $count=mysql_num_rows($result); /* If email is alrady in use. */ if($count==1){ $err = '1'; $msg = "Sorry but this email address has already been used.<br>Please <strong><a href='lostpassword.php?email=$email'>CLICK HERE</a></strong> to resend your password. "; } else { if($cvsel=='yes'){ //UPLOAD FILE $target = "../cv/"; $target = $target . basename( $_FILES['cv']['name']) ; $filename = basename( $_FILES['cv']['name']); if(move_uploaded_file($_FILES['cv']['tmp_name'], $target)) { $msg = "The file ". basename( $_FILES['cv']['name']). " has been uploaded"; } else { $err = '1'; $msg = "There was an error uploading the file " . basename( $_FILES['cv']['name']) . " please try again!"; } } else { } //set the random id length $random_id_length = 10; $rnd_id = crypt(uniqid(rand(),1)); $rnd_id = strip_tags(stripslashes($rnd_id)); $rnd_id = str_replace(".","",$rnd_id); $rnd_id = strrev(str_replace("/","",$rnd_id)); $rnd_id = substr($rnd_id,0,$random_id_length); $reg_date = date("Y-m-d"); $password = $rnd_id; mysql_query("INSERT INTO candidateinfo (candid, password, reg_date, title, firstname, surname, dob, address, tel, mob, email, prefercontact, nationality, legalent, english, convict, convictiondetails, cvattached, work1, work2, work3, worktype, ftpt, avoidwork, avoidemployer, datesavailable, qualifications, experience, licences, transport, comments, searchhow, signature) VALUES ('$candid', '$password', '$reg_date', '$title', '$firstname', '$surname', '$dob', '$address', '$tel', '$mob', '$email', '$prefercontact', '$nationality', '$legalent', '$english', '$convict', '$convictiondetails', '$cvattached', '$work1', '$work2', '$work3', '$worktype', '$ftpt', '$avoidwork', '$avoidemployer', '$datesavailable', '$qualifications', '$experience', '$licences', '$transport', '$comments', '$searchhow', '$signature');"); include ("candemail.php"); include ("email2client.php"); } ?> is there a way that after the mysql insert string, and before the 2 includes, that I can find the file on the server and rename it? How would you do it? any help would be awsum Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/ Share on other sites More sharing options...
mtoynbee Posted November 25, 2008 Share Posted November 25, 2008 Use PHP function rename() http://uk.php.net/rename Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698602 Share on other sites More sharing options...
wmguk Posted November 25, 2008 Author Share Posted November 25, 2008 hmm, ok thats cool, i tried: rename ("http://www.domain.co.uk/admin/cv" . basename( $_FILES['cv']['name']), "http://www.domain.co.uk/admin/cv" . "$candid" . mysql_insert_id() ); however that didnt rename it... any thoughts? Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698759 Share on other sites More sharing options...
.josh Posted November 25, 2008 Share Posted November 25, 2008 silly question, but why not just name it what you want it to be named in the first place, up in the move_uploaded_file? But anyways, try using $target as the first argument of rename. Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698767 Share on other sites More sharing options...
flyhoney Posted November 25, 2008 Share Posted November 25, 2008 Maybe this? $target = "../cv/"; rename ($target . basename( $_FILES['cv']['name']), $target . $candid . mysql_insert_id() ); Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698768 Share on other sites More sharing options...
flyhoney Posted November 25, 2008 Share Posted November 25, 2008 silly question, but why not just name it what you want it to be named in the first place, up in the move_uploaded_file? But anyways, try using $target as the first argument of rename. I think hes saying he needs to name it based on the mysql_insert_id, but he doesn't INSERT until after he has moved the uploaded file. Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698769 Share on other sites More sharing options...
.josh Posted November 25, 2008 Share Posted November 25, 2008 well he could just reorganize his code. But okay, since his $target already has what your code suggests (from earlier), no need to reassign stuff, just use $target Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698770 Share on other sites More sharing options...
wmguk Posted November 25, 2008 Author Share Posted November 25, 2008 Maybe this? $target = "../cv/"; rename ($target . basename( $_FILES['cv']['name']), $target . $candid . mysql_insert_id() ); ah ha!!! this works except it loses the file type (.doc / .jpg etc) Yeah my main issue was i need the mysql_insert_id() to code the new filename, and can only do that after the insert works... Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698774 Share on other sites More sharing options...
wmguk Posted November 25, 2008 Author Share Posted November 25, 2008 scrub that - i added: $fileend = substr("$filename", -3); $target = "../cv/"; rename ($target . basename( $_FILES['cv']['name']), $target . $candid . mysql_insert_id() . "." . $fileend); now working thank you Link to comment https://forums.phpfreaks.com/topic/134213-solved-rename-an-uploaded-file/#findComment-698779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.