We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How do I rename an uploaded file?

Donnie Walker
on
Medium Priority
240 Views
Last Modified: 2013-12-12
I have the attached script that works fine. What I need is for it to rename the uploaded file (from the text field "new_image") to the baby name field (in "bname") and male it lowercase as well.

I'm already creating a random name for the start of each upload...but I would like the posted baby name to be the file name after the random name.

So, if the user uploads "sals%20daasd.jpg" I would like it to rename that to "random_babyname.jpg".

How would this be done?
<?php include("includes/dbconn.php"); ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="en-gb" />
    <meta http-equiv="Pragma" content="no-cache"> 
    <title>Logansport Memorial Hospital Nursery</title>
      <link rel="stylesheet" href="../includes/style.css" />
      <script src="../includes/gen_validatorv3.js"  language="JavaScript"></script>
      <script type="text/javascript" src="../includes/calendarDateInput.js" language="JavaScript"></script>
</head>
 
<body>
 
<form name="myform" action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
 
<div id="container">
    <table>
        <tr><td align="right" nowrap><b>Baby Name:</b></td><td><input type="text" size="20" name="bname" maxlength="25"></td></tr>
        <tr><td align="right" nowrap><b>Birth Date:</b></td><td><script>DateInput('birthday', true)</script></td></tr>
        <tr><td align="right" nowrap><b>Birth Time:</b></td><td>
        <select name="bhour">
        <option value="1">01</option>
        <option value="2">02</option>
        <option value="3">03</option>
        <option value="4">04</option>
        <option value="5">05</option>
        <option value="6">06</option>
        <option value="7">07</option>
        <option value="8">08</option>
        <option value="9">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        </option>
        </select>
         <b>:</b>
        <select name="bmin">
        <option value="00">00</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
        <option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
        <option value="32">32</option>
        <option value="33">33</option>
        <option value="34">34</option>
        <option value="35">35</option>
        <option value="36">36</option>
        <option value="37">37</option>
        <option value="38">38</option>
        <option value="39">39</option>
        <option value="40">40</option>
        <option value="41">41</option>
        <option value="42">42</option>
        <option value="43">43</option>
        <option value="44">44</option>
        <option value="45">45</option>
        <option value="46">46</option>
        <option value="47">47</option>
        <option value="48">48</option>
        <option value="49">49</option>
        <option value="50">50</option>
        <option value="51">51</option>
        <option value="52">52</option>
        <option value="53">53</option>
        <option value="54">54</option>
        <option value="55">55</option>
        <option value="56">56</option>
        <option value="57">57</option>
        <option value="58">58</option>
        <option value="59">59</option>
        </option>
        </select>
        <select name="bpid">
        <option value="AM">AM</option>
        <option value="PM">PM</option>
        </select>
</td></tr>
        <tr><td align="right"><b>Weight:</b></td><td><input type="text" size="2" name="lbs" maxlength="5"> lbs <input type="text" size="2" name="oz" maxlength="5"> oz</td></tr>
        <tr><td align="right"><b>Length:</b></td><td><input type="text" size="2" name="blength" maxlength="5"> Inches</td></tr>
        <tr><td align="right"><b>Parent(s):</b></td><td><input type="text" size="20" name="bparents"></td></tr>
        <tr><td align="right"><b>Doctor:</b></td><td><input type="text" size="20" name="bdoctor"></td></tr>
        <tr><td align="right"><b>Photo:</b></td><td nowrap><input name="new_image" id="new_image" size="30" type="file" class="submit" />
        <input name="submit" type="submit" class="submit" value="Upload"></td></tr>
    </table>
</div>
</form>
 
<script language="JavaScript" type="text/javascript">
 var frmvalidator = new Validator("myform");
 frmvalidator.addValidation("bname","req","Please enter the BABY NAME!");
 frmvalidator.addValidation("birthday","req","Please select the BABY'S BIRTHDAY!");
 frmvalidator.addValidation("lbs","req","Please enter the baby's WEIGHT IN POUNDS!");
 frmvalidator.addValidation("oz","req","Please enter the baby's WEIGHT IN OUNCES!");
 frmvalidator.addValidation("blength","req","Please enter the baby's LENGTH IN INCHES!");
 frmvalidator.addValidation("bparents","req","Please enter the BABY'S PARENTS FIRST NAMES!");
 frmvalidator.addValidation("bdoctor","req","Please enter the DELIVERY DOCTOR'S NAME!");
 frmvalidator.addValidation("new_image","req","Please select the BABY'S PHOTO to UPLOAD!");
</script>
 
 
<?php
 
        if(isset($_POST['submit'])){
 
          if (isset ($_FILES['new_image'])){
 
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "../photos/".$imagename;
              move_uploaded_file($source, $target);
 
              $ran = rand ();
 
              $imagepath = $imagename;
              $save = "../photos/". $ran . "_". $imagepath; //This is the new file you saving
              $file = "../photos/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 400; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 80) ; 
 
              $save = "../photos/tn_". $ran ."_". $imagepath; //This is the new file you saving
              $file = "../photos/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 150; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 80) ; 
 
           		$bname = $_POST['bname'];
           		$bdate = $_POST['birthday'];
           		$btime = $_POST['bhour'].":".$_POST['bmin']." ".$_POST['bpid'];
           		$length = $_POST['blength']." inches";
           		$weight = $_POST['lbs']." lbs ".$_POST['oz']. "oz";
           		$parents = $_POST['bparents'];
           		$doctor = $_POST['bdoctor'];
 
			$byear = substr($bdate,0,4);
			$bmonth = substr($bdate,4,2);
			$bday = substr($bdate,6,2);
 
                  $photo = $ran . "_" . $imagepath;
 
		$sql="INSERT INTO jos_babies (BabyName,BirthMonth,BirthDay,BirthYear,BirthTime,Weight,Length,Parents,Doctor,Photo)VALUES('$bname','$bmonth','$bday','$byear','$btime','$weight','$length','$parents','$doctor','$photo')";
            $result=mysql_query($sql, $dbconn);
 
 
          }
        }
 
 
?>
</body>
</html>

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
When you use imagejpeg() you can specify the name.  

Author

Commented:
How you I use that in my code?
Most Valuable Expert 2011
Author of the Year 2014

Commented:
See changes near the bottom.  I put some whitespace and comments around the parts I changed. ~Ray
<?php include("includes/dbconn.php"); ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="en-gb" />
    <meta http-equiv="Pragma" content="no-cache"> 
    <title>Logansport Memorial Hospital Nursery</title>
      <link rel="stylesheet" href="../includes/style.css" />
      <script src="../includes/gen_validatorv3.js"  language="JavaScript"></script>
      <script type="text/javascript" src="../includes/calendarDateInput.js" language="JavaScript"></script>
</head>
 
<body>
 
<form name="myform" action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
 
<div id="container">
    <table>
        <tr><td align="right" nowrap><b>Baby Name:</b></td><td><input type="text" size="20" name="bname" maxlength="25"></td></tr>
        <tr><td align="right" nowrap><b>Birth Date:</b></td><td><script>DateInput('birthday', true)</script></td></tr>
        <tr><td align="right" nowrap><b>Birth Time:</b></td><td>
        <select name="bhour">
        <option value="1">01</option>
        <option value="2">02</option>
        <option value="3">03</option>
        <option value="4">04</option>
        <option value="5">05</option>
        <option value="6">06</option>
        <option value="7">07</option>
        <option value="8">08</option>
        <option value="9">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        </option>
        </select>
         <b>:</b>
        <select name="bmin">
        <option value="00">00</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
        <option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
        <option value="32">32</option>
        <option value="33">33</option>
        <option value="34">34</option>
        <option value="35">35</option>
        <option value="36">36</option>
        <option value="37">37</option>
        <option value="38">38</option>
        <option value="39">39</option>
        <option value="40">40</option>
        <option value="41">41</option>
        <option value="42">42</option>
        <option value="43">43</option>
        <option value="44">44</option>
        <option value="45">45</option>
        <option value="46">46</option>
        <option value="47">47</option>
        <option value="48">48</option>
        <option value="49">49</option>
        <option value="50">50</option>
        <option value="51">51</option>
        <option value="52">52</option>
        <option value="53">53</option>
        <option value="54">54</option>
        <option value="55">55</option>
        <option value="56">56</option>
        <option value="57">57</option>
        <option value="58">58</option>
        <option value="59">59</option>
        </option>
        </select>
        <select name="bpid">
        <option value="AM">AM</option>
        <option value="PM">PM</option>
        </select>
</td></tr>
        <tr><td align="right"><b>Weight:</b></td><td><input type="text" size="2" name="lbs" maxlength="5"> lbs <input type="text" size="2" name="oz" maxlength="5"> oz</td></tr>
        <tr><td align="right"><b>Length:</b></td><td><input type="text" size="2" name="blength" maxlength="5"> Inches</td></tr>
        <tr><td align="right"><b>Parent(s):</b></td><td><input type="text" size="20" name="bparents"></td></tr>
        <tr><td align="right"><b>Doctor:</b></td><td><input type="text" size="20" name="bdoctor"></td></tr>
        <tr><td align="right"><b>Photo:</b></td><td nowrap><input name="new_image" id="new_image" size="30" type="file" class="submit" />
        <input name="submit" type="submit" class="submit" value="Upload"></td></tr>
    </table>
</div>
</form>
 
<script language="JavaScript" type="text/javascript">
 var frmvalidator = new Validator("myform");
 frmvalidator.addValidation("bname","req","Please enter the BABY NAME!");
 frmvalidator.addValidation("birthday","req","Please select the BABY'S BIRTHDAY!");
 frmvalidator.addValidation("lbs","req","Please enter the baby's WEIGHT IN POUNDS!");
 frmvalidator.addValidation("oz","req","Please enter the baby's WEIGHT IN OUNCES!");
 frmvalidator.addValidation("blength","req","Please enter the baby's LENGTH IN INCHES!");
 frmvalidator.addValidation("bparents","req","Please enter the BABY'S PARENTS FIRST NAMES!");
 frmvalidator.addValidation("bdoctor","req","Please enter the DELIVERY DOCTOR'S NAME!");
 frmvalidator.addValidation("new_image","req","Please select the BABY'S PHOTO to UPLOAD!");
</script>
 
 
<?php
 
        if(isset($_POST['submit'])){
 
 
 
 // SEGMENT OF CODE MOVED UP FROM BELOW
              $bname = $_POST['bname'];
              $bdate = $_POST['birthday'];
              $btime = $_POST['bhour'].":".$_POST['bmin']." ".$_POST['bpid'];
              $length = $_POST['blength']." inches";
              $weight = $_POST['lbs']." lbs ".$_POST['oz']. "oz";
              $parents = $_POST['bparents'];
              $doctor = $_POST['bdoctor'];
  
              $byear = substr($bdate,0,4);
              $bmonth = substr($bdate,4,2);
              $bday = substr($bdate,6,2);
// SEGMENT OF CODE MOVED UP FROM BELOW
 
 
 
 
 
          if (isset ($_FILES['new_image'])){
 
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "../photos/".$imagename;
              move_uploaded_file($source, $target);
 
              $ran = rand ();
 
              $imagepath = $imagename;
 
 
 
 
// CHANGE THE NEXT LINE TO PUT IN THE $bname FIELD WHERE YOU WANT IT              
              $save = "../photos/". "$ran" . "_". $imagepath; //This is the new file you saving
 
 
 
              
              $file = "../photos/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 400; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 80) ; 
 
              $save = "../photos/tn_". $ran ."_". $imagepath; //This is the new file you saving
              $file = "../photos/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 150; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 80) ; 
 
 
                  $photo = $ran . "_" . $imagepath;
 
                $sql="INSERT INTO jos_babies (BabyName,BirthMonth,BirthDay,BirthYear,BirthTime,Weight,Length,Parents,Doctor,Photo)VALUES('$bname','$bmonth','$bday','$byear','$btime','$weight','$length','$parents','$doctor','$photo')";
            $result=mysql_query($sql, $dbconn);
 
 
          }
        }
 
 
?>
</body>
</html>

Open in new window

Author

Commented:
It still has the original uploaded filename. I need it to replace the uploaded filename with the baby name.

I changed this line like you commented:

              $save = "../photos/". "$ran" . "_". $bname ."_". $imagepath; //This is the new file you saving

So, i guess I need a way to change $imagepath to $bname?



Most Valuable Expert 2011
Author of the Year 2014

Commented:
That sounds correct.  I am not sure exactly how you want to name the file, but you're on the right track.
Did you catch the lines at 137 to 149 in my code snippet?

Author

Commented:
I want to take whatever they upload and rename it to $ran ."_". $bname .".jpg

But I don't know how to do that.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
I think this is what you mean.  Near line 169 in the code snippet above.
// CHANGE THE NEXT LINE TO PUT IN THE $bname FIELD WHERE YOU WANT IT              
$save = "../photos/". "$ran" . "_" . $bname . '.jpg'; //This is the new file you saving

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Or, if the extension is always going to be .jpg could I do this?

$save = "../photos/". "$ran" . "_". $bname .".jpg";

Author

Commented:
I type too slow I guess. :)
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Looks like we are on the same wavelength!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points -- glad you nailed it! ~Ray
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.