[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I rename an uploaded file?

Posted on 2009-02-16
11
Medium Priority
?
230 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

0
Comment
Question by:Donnie Walker
  • 6
  • 5
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23654517
When you use imagejpeg() you can specify the name.  
0
 

Author Comment

by:Donnie Walker
ID: 23654714
How you I use that in my code?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23654791
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Donnie Walker
ID: 23654953
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?



0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23655312
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?
0
 

Author Comment

by:Donnie Walker
ID: 23655351
I want to take whatever they upload and rename it to $ran ."_". $bname .".jpg

But I don't know how to do that.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 23655383
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

0
 

Author Comment

by:Donnie Walker
ID: 23655425
Or, if the extension is always going to be .jpg could I do this?

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

Author Comment

by:Donnie Walker
ID: 23655427
I type too slow I guess. :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23655452
Looks like we are on the same wavelength!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23656008
Thanks for the points -- glad you nailed it! ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question