title: Uploaded and resized photo in Codeigniter is not available in view until I refresh .. is my view loading "too fast"?
body: <p>I'm having problems with a photo upload system in Codeigniter.</p>
  
  <p>I'm uploading and resizing a photo during registration in my controller, and once that goes through, I load an "upload successful" view where I want to display the photo again.</p>
  
  <p>The problem is, </p>
  
  <ul>
  <li>I step into a "fail" loop that actually seems to not be failing</li>
  </ul>
  
  <p><em>and</em> </p>
  
  <ul>
  <li>the first time I load my view, the photo isn't displaying (although it has been uploaded, resized, and the path is in my DB).  But if I refresh the page, it does display correctly.</li>
  </ul>
  
  <p>Here is the controller .. check the comments close to the bottom, because the top part is basically just the resizing logic and (although it is kind of ugly right now) it does work</p>
  
  <pre><code>public function do_upload()
  {
      $config['upload_path'] = './upload_pic/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '100';
      $config['max_width']  = '1200';
      $config['max_height']  = '768';
      $_maxResizedWidth = '242';
  
      $this-&gt;load-&gt;library('upload', $config);
  
      if ( $this-&gt;upload-&gt;do_upload() )
      {
          $this-&gt;data['upload_data'] = $this-&gt;upload-&gt;data();
          $this-&gt;data['title'] = "Upload Successful";
  
           switch($this-&gt;data['upload_data']['file_type']){
              case "image/jpg":
                  $_file_suffix = ".jpg";
                  break;
              case "image/jpeg":
                  $_file_suffix = ".jpg";
                  break;
              case "image/png":
                  $_file_suffix = ".png";
                  break;
              case "image/gif":
                  $_file_suffix = ".gif";
                  break;
              case "image/pjpeg":
                  $_file_suffix = ".jpg";
                  break;
           }
  
          $_fileName = $this-&gt;data['upload_data']['file_name']; 
          $_oldUploadPath = $config['upload_path'] . $_fileName;
          $_random_key = strtotime(date('Y-m-d H:i:s'));
          $_newUploadPath = $config['upload_path'] . 'resize_' . $_random_key . $_file_suffix;
          rename( $_oldUploadPath, $_newUploadPath );
  
          $_oldSize = getimagesize($_newUploadPath);
          $_oldWidth = $_oldSize[0];
  
          if($_oldWidth &gt; $_maxResizedWidth){
              $_scale = $_maxResizedWidth/$_oldWidth;
              $_oldHeight = $_oldSize[1];
              $_imageType = image_type_to_mime_type($_oldSize[2]);
              $_newWidth = $_oldWidth * $_scale;
              $_newHeight = $_oldHeight * $_scale;
              $_resizedImage = imagecreatetruecolor($_newWidth, $_newHeight);
              switch($_imageType) {
                  case "image/gif":
                      $_source=imagecreatefromgif($_newUploadPath); 
                      break;
                  case "image/pjpeg":
                  case "image/jpeg":
                  case "image/jpg":
                      $_source=imagecreatefromjpeg($_newUploadPath); 
                      break;
                  case "image/png":
                  case "image/x-png":
                      $_source=imagecreatefrompng($_newUploadPath); 
                      break;
              }
              imagecopyresampled($_resizedImage, $_source, 0, 0, 0, 0, $_newWidth, $_newHeight, $_oldWidth, $_oldHeight);
              switch($_imageType) {
                  case "image/gif":
                      imagegif($_resizedImage,$_newUploadPath); 
                      break;
                  case "image/pjpeg":
                  case "image/jpeg":
                  case "image/jpg":
                      imagejpeg($_resizedImage,$_newUploadPath,90); 
                      break;
                  case "image/png":
                  case "image/x-png":
                      imagepng($_resizedImage,$_newUploadPath);  
                      break;
              }
          }
  
          $_newUploadPathWithoutDot = substr($_newUploadPath,1);
  
          $this-&gt;load-&gt;model('Member_model');
          $ID = $this-&gt;the_user-&gt;id;
  
               /// ****************************************************************************
               /// I step into this next if loop even though the upload_photo method does work.  
              /// In my view I get the "there was a problem" .. but the DB has been correctly updated
  
          if(!$this-&gt;Member_model-&gt;upload_photo( $ID, $_newUploadPathWithoutDot));
          {
              $uploadData = $this-&gt;upload-&gt;data();
              $this-&gt;data['message'] = "There was a problem entering your photo in the database ==";
          };
  
                /// And this is the view that's called
          $this-&gt;load-&gt;view('upload_big_success_view', $this-&gt;data);
      }
      else
      {
          $this-&gt;data['message'] = "there was a problem " . $this-&gt;upload-&gt;display_errors() . dirname(__FILE__);
          $this-&gt;data['title'] = "Upload Unsuccessful";
          $this-&gt;load-&gt;view('profile_photo_view', $this-&gt;data);
      }
  }
  </code></pre>
  
  <p>(I know there is a lot of messy in there, and I'll give it a refactor).</p>
  
  <p>It almost seems like the image is "not ready" when the view first loads, even though everything has worked - the upload, the resize, the filepath is entered in the DB... nothing has thrown any errors until it gets to that last loop and it seems to think "->upload_photo()" has failed when it hasn't.  And then when I call the view the first time my ..</p>
  
  <pre><code>&lt;img src"&lt;?php echo $the_user-&gt;large_image_location; ?&gt;" &gt; 
  </code></pre>
  
  <p>.. produces no output.  But after a simple refresh, it does.  Although the "there was a problem message" is still displaying in the view.</p>
  
  <p>BTW - here is the model.  Nothing fancy.</p>
  
  <pre><code>public function upload_photo($ID, $_newUploadPath)
  {
      $sql = "UPDATE users SET large_image_location=? WHERE id=?";
      $query = $this-&gt;db-&gt;query($sql, array($_newUploadPath, $ID));
      return $query;  
  }
  </code></pre>
  
  <p>So any ideas?</p>