title: Undefined variable not allowing me to insert file into database table column
body: <p>I'm uploading audio files into a directory and storing the file name info in database. I have done this successfully.</p>
  
  <p>Next I wanted to create a default image for each audio file, by use PHP to add the audio files Title to the image and then tack on a unique id to the file name. I have done this successfully. I learned this by <a href="http://papermashup.com/php-gd-generate-an-image-with-text-and-font-embedding/" rel="nofollow noreferrer">following this tutorial.</a></p>
  
  <p>Example Image:</p>
  
  <p><img src="https://i.stack.imgur.com/bjAf7.jpg" alt="enter image description here"></p>
  
  <p>The problem I'm having is getting the audio files image to insert into the database column on upload.
  Here is a database image. I need the file name of the newly created audio image to go into the <strong>rt_file</strong> column. So I can echo it out later.</p>
  
  <p><img src="https://i.stack.imgur.com/7Fm0N.jpg" alt="enter image description here"></p>
  
  <p>I was hoping to get some help on where I'm going wrong.</p>
  
  <p>The current error I get is: <strong>Undefined variable: rt_file</strong>.
  Which I don't understand why...? does this not define it like I have it defined above the insert statement like this:</p>
  
  <pre><code>$rt_file = preg_replace('/\s+/', '-', "../ringtones/rtimage/".($ringtone_image[0]['ring_name'])."-".substr(md5(microtime()),rand(0,26),5).".jpg");
  </code></pre>
  
  <p>I really hate to post this much code I know it's frowned upon, so I apologize in advance, but I'm not sure where the problem lies.</p>
  
  <p>If someone has some time to look through this that would be great, if not I totally  understand.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/core/database.php");
  
  
  $ringDir= "../ringtones/";
  
  if(!is_dir($ringDir)){
  
      mkdir('../ringtones/', 0775);
  }
  
  
  
  .   
  if (isset($_FILES['file'])) {
  
      if (empty($_FILES['file']['name'])) {
  
              ?&gt;
  &lt;div class="add-errors"&gt;Please add an audio file!&lt;/div&gt;
  &lt;?php 
  
      }   else {
  
          $name       =   $_FILES['file']['name'];
          $filename   =   preg_replace("/\.[^.]+$/", "", $name);
          $temp       =   $_FILES['file']['tmp_name'];
          $type       =   $_FILES['file']['type'];
          $size       =   $_FILES['file']['size'];
          $ext        =   strtolower(end(explode('.', $name)));
  
    $upload   =   substr(md5(microtime()),rand(0,26),5);
  
          // Restrictions for uploading
          $allowed    =   array(
          'audio/mp3',
          'audio/mp4',
          'audio/mpeg',
          'audio/ogg',
          'audio/opus',
          'audio/webm');
  
          // Recognizing the extension
          switch($type){
  
              case 'audio/mp3':
                      $ext= '.mp3';
              break;
  
              case 'audio/mp4':
                      $ext= '.mp4';
              break;
  
              case 'audio/mpeg':
                      $ext= '.mpeg';
              break;
  
              case 'audio/ogg':
                      $ext= '.ogg';
              break;
  
              case 'audio/opus':
                      $ext= '.opus';
              break;
  
              case 'audio/webm':
                      $ext= '.webm';
              break;
  
          }
  
          // upload variables
          $ring_path      =   $ringDir.$filename."-".$upload.$ext;
  
  
  
  
  
          // check if extension is allowed.
          if (in_array($type, $allowed)) {
  
              // Checking the size of the audio file.
                  if ($size &lt;= 5242880) {
  
                          // Move the original file aswell.
                          move_uploaded_file($temp, $ring_path);
  
  
  
  /*#############THIS IS WHERE I'M ADDING THE TEXT TO THE DEFAULT IMAGE###########
  ################################################################################*/
  
  // link to the font file no the server
  $fontname = '../fonts/Roboto-Regular.ttf';
  // controls the spacing between text
  $i=30;
  //JPG image quality 0-100
  $quality = 100;
  
  function create_image($ringtone_image){
  
          global $fontname;   
          global $quality;
          $rt_file = preg_replace('/\s+/', '-', "../ringtones/rtimage/".($ringtone_image[0]['ring_name'])."-".substr(md5(microtime()),rand(0,26),5).".jpg");  
  
       //if the file already exists dont create it again just serve up the original   
      if (!file_exists($rt_file)) {   
      }
  
              // define the base image that we lay our text on
              $im = imagecreatefromjpeg("../ringtones/ringtone-image.jpg");
  
              // setup the text colours
              $color['white'] = imagecolorallocate($im, 255, 255, 255);
  
              // this defines the starting height for the text block
              $height = 50;
              $y = imagesy($im) - $height;
  
          // loop through the array and write the text    
          foreach ($ringtone_image as $value){
              // center the text in our image - returns the x value
              $x = center_text($value['ring_name'], $value['font-size']);
  
              imagettftext($im, $value['font-size'], 0, $x, $y, $color[$value['color']], $fontname,$value['ring_name']);
              // add 32px to the line height for the next text block
              //$i = $i+32;   
  
          }
              // create the image
              imagejpeg($im, $rt_file, $quality);
  
      //}
  
          return $rt_file;    
  }
  
  function center_text($string, $font_size){
  
              global $fontname;
  
              $image_width = 550;
              $dimensions = imagettfbbox($font_size, 0, $fontname, $string);
  
              return ceil(($image_width - $dimensions[4]) / 2);               
  }
  
  
  
      $ringtone_image = array(
  
          array(
              'ring_name'=&gt; 'Add Ringtone Title', 
              'font-size'=&gt;'16',
              'color'=&gt;'white'),
  
      );
  
  
  
      $ringtone_image = array(
  
          array(
              'ring_name'=&gt; $_POST['ring_name'], 
              'font-size'=&gt;'16',
              'color'=&gt;'white'),
  
  
      );      
  
  
  
  // run the script to create the image
  $filename = create_image($ringtone_image);
  
  /*#############END TEXT TO DEFAULT IMAGE###########
  ###################################################*/
  
  
                          try {
  
                              $ring_id            =   Input::get('ring_id');
                              $creation           =   date('Y-m-d H:i:s');
                              $ring_name          =   Input::get('ring_name');
                              $category           =   Input::get('category_id');
                              $ring_path          =   $ring_path;
                              $rt_file            =   $rt_file;   
  
  
                              $insertdata = DB::getInstance()-&gt;insert('ringtones', array(
                                  'ring_id'            =&gt; $ring_id,
                                  'creation'           =&gt; $creation,
                                  'ring_name'          =&gt; $ring_name,
                                  'category_id'        =&gt; $category,
                                  'ring_path'          =&gt; str_replace("../ringtones/", "", $ring_path),
                                  'rt_file'           =&gt; str_replace("../ringtones/rtimages/", "", $rt_file),
                              ));
  
  
                              if(!$insertdata) {
  
                                  ?&gt;
  &lt;div class="add-errors"&gt;There was a problem uploading the ringtone!&lt;/div&gt;
  &lt;?php 
  
                              } else {
  
                                  ?&gt;
  &lt;div class="add-message"&gt;Your ringtone has been uploaded! &lt;a href="add-ringtone.php"&gt;Add another&lt;/a&gt; or &lt;a href="../index.php"&gt;Return Home&lt;/a&gt; &lt;/div&gt;
  &lt;?php 
                              }
  
                          } catch(Exception $e) {
                              ?&gt;
  &lt;div class="add-errors"&gt;
          &lt;?php die($e-&gt;getMessage()); ?&gt;
  &lt;/div&gt;
  &lt;?php 
                          }
  
                  } else {
                      ?&gt;
  &lt;div class="add-errors"&gt;Your ringtone size is too big!&lt;/div&gt;
  &lt;?php
                  }
  
              }
  
           else {
              ?&gt;
  &lt;div class="add-errors"&gt;Your have uploaded a forbidden extension!&lt;/div&gt;
  &lt;?php
  
  }
  
  }
  
  }
  
  ?&gt;
  &lt;section&gt;
          &lt;h1&gt;Audio Upload&lt;/h1&gt;
          &lt;p&gt;Use the form below to upload new ringtone&lt;/p&gt;
          &lt;form action="" method="post" enctype="multipart/form-data"&gt;
                  &lt;label for="ring_name"&gt;Ringtone Title&lt;/label&gt;
                  &lt;input type="text" name="ring_name" maxlength="42" placeholder="Give this field a title"&gt;
                  &lt;label for="category_id"&gt;Choose a category&lt;/label&gt;
                  &lt;select name="category_id"&gt;
                          &lt;option value=""&gt;Please Choose&lt;/option&gt;
                          &lt;option value="1"&gt;Option 1&lt;/option&gt;
                          &lt;option value="2"&gt;Option 2&lt;/option&gt;
                          &lt;option value="3"&gt;Option 3&lt;/option&gt;
                          &lt;option value="4"&gt;Option 4&lt;/option&gt;
                          &lt;option value="5"&gt;Option 5&lt;/option&gt;
                          &lt;option value="6"&gt;Option 6&lt;/option&gt;
                  &lt;/select&gt;
                  &lt;label for="file"&gt;Ringtone&lt;/label&gt;
                  &lt;input type="file" name="image" &gt;
                  &lt;p id="size"&gt;Maximum file size of 5MB&lt;/p&gt;
                  &lt;input type="submit" value="Upload"&gt;
          &lt;/form&gt;
  &lt;/section&gt;
  </code></pre>