title: PHP - get base64 img string decode and save as jpg (resulting empty image )
body: <p>hi i'm actually sending a base64 image string trough ajax to a php script which just decodes string and save content as .jpg file.</p>
  
  <p>But the result is an empty image.</p>
  
  <p>How can this be possible?</p>
  
  <p>php script:</p>
  
  <pre><code>$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4');
              foreach ($uploadedPhotos as $file) {
                  if($this-&gt;input-&gt;post('photo_1')){
                       $photoTemp = base64_decode($this-&gt;input-&gt;post('photo_1'));
  
  
                      /*Set name of the photo for show in the form*/
                      $this-&gt;session-&gt;set_userdata('upload_'.$file,'ant');
                      /*set time of the upload*/
                      if(!$this-&gt;session-&gt;userdata('uploading_on_datetime')){
                       $this-&gt;session-&gt;set_userdata('uploading_on_datetime',time());
                      }
                       $datetime_upload = $this-&gt;session-&gt;userdata('uploading_on_datetime',true);
                      /*create temp dir with time and user id*/
                      $new_dir = 'temp/user_'.$this-&gt;session-&gt;userdata('user_id',true).'_on_'.$datetime_upload.'/';
                      @mkdir($new_dir);
                      /*move uploaded file with new name*/
                      @file_put_contents( $new_dir.$file.'.jpg',$photoTemp);
  
  
              }
  </code></pre>
  
  <p>For ajax it is ok because, echo $photoTemp returns the string.</p>
  
  <p>i tried <code>var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));</code> and it returns <code>bool(true)</code> since the image is saved but there is no content in the image :( empty image</p>
  
  <blockquote>
    <p>for empty image i mean , file is created and named, and it has the same
    size of the content i pass to php, but when i try to open that image
    to preview it says, file can't be opened because corrupted or bad file type format </p>
  </blockquote>
  
  <p>anyway this is the JS that takes photo as base64 and send that to php:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var _min_width = 470;
  var _min_height = 330;
  var _which;
  var _fyle_type;
  var  io;
  var allowed_types = new Array('image/png','image/jpg','image/jpeg');
  if (typeof(FileReader) === 'function'){
  $('input[type="file"]').on('change', function(e) {
      var _file_name = $(this).val();
      $('.'+_which+'_holder').text(_file_name);
      var file = e.target.files[0];
  
      if (!in_array(file.type,allowed_types) || file.length === 0){
          notify("You must select a valid image file!",false,false); 
          return;
      }
  
      if(file.size &gt; 3145728 /*3MB*/){
          notify("&lt;?php echo lang('each-photo-1MB'); ?&gt;",false,false); 
          return;
      }
      notify_destroy();
  
      var reader = new FileReader();
      reader.onload = fileOnload;
    reader.readAsDataURL(file);
  
  
  });
  
  function fileOnload(e) {
      var img = document.createElement('img');
      img.src = e.target.result;
  
      img.addEventListener('load', function() {
          if(img.width &lt; _min_width || img.height &lt; _min_height ){
          notify("&lt;?php echo lang('each-photo-1MB'); ?&gt;",false,false); 
          return;
          }
  
  
          $.ajax({
              type:'post',
              dataType:'script',
              data:{photo_1:e.target.result},
              url:_config_base_url+'/upload/upload_photos',
              progress:function(e){
                  console.log(e);
              },
              success:function(d){
                  $('body').append('&lt;img src="'+d+'"/&gt;');
              }
           });
  
  
      });
  
  }
  }
  &lt;/script&gt;
  </code></pre>