<div class="form-group">
    <label for='image' class="col-sm-2 control-label">Leader Board</label>
    <div class="col-sm-10">
        <input id= "txt_lead" type = "text" name="image_lead" class="form-control input-sm" onclick ="javascript:document.getElementById('file_lead').click();">
        <input id = "file_lead" type= "file" accept="image/*" name="file_ads_lead" style='visibility: hidden;' onchange="ChangeText(this, 'txt_lead');"/>
        <?php echo form_error('image_lead', '<small class="pull-right req">', '</small>');?>
    </div>
</div>



<script>
    function ChangeText(oFileInput, sTargetID) {   
    document.getElementById(sTargetID).value = oFileInput.value.replace("C:\\fakepath\\", "");;
    }
</script>

I'm trying to identify image size by using $_FILES['file_ads_lead']['temp_name'], but i got nothing.
Any idea why?
ps: input file tag is hidden

Recommended Answers

All 4 Replies

You've got one line of PHP here that is for a form error. The code that actually gets the size of the image would be helpful. And when you say "i got nothing.", what do you mean? Do you get an error message? does the $_FILES array even exist? etc.

try this:

      $uploadpath = 'uploads/';      // directory to store the uploaded files
      $max_size = 2600;          // maximum file size, in KiloBytes
      $alwidth = 1500;            // maximum allowed width, in pixels
      $alheight = 2000;           // maximum allowed height, in pixels
      $allowtype = array('bmp', 'gif', 'jpg', 'pdf', 'png');        // allowed extensions

      if(isset($_FILES['uploadedfile']) && strlen($_FILES['uploadedfile']['name']) > 1) {
      $uploadpath = $uploadpath . basename( $_FILES['uploadedfile']['name']);       // gets the file name
      $sepext = explode('.', strtolower($_FILES['uploadedfile']['name']));
      $type = end($sepext);       // gets extension

      list($width, $height) = getimagesize($_FILES['uploadedfile']['tmp_name']);     // gets image width and height
      $err = '';         // to store the errors

      // Checks if the file has allowed type, size, width and height (for images)
      if(!in_array($type, $allowtype)) 
      $err .= '<div class="style7">The file: <b>'. $_FILES['uploadedfile']['name']. '</b> not has the allowed extension type.</div>';

      if($_FILES['uploadedfile']['size'] > $max_size*1000) 
      $err .= '<br/><div class="style7">Maximum file size must be: '. $max_size. ' KB.</div>';

      if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) 
      $err .= '<br/><div class="style7">The maximum Width x Height must be: '. $alwidth. ' x '. $alheight . '</div>';

      // If no errors, upload the image, else, output the errors
      if($err == '') {
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadpath)) { 
          echo '<div class="style7">File <b>'. basename( $_FILES['uploadedfile']['name']). '</b> successfully uploaded!';
          echo '<br/>File type: <b>'. $_FILES['uploadedfile']['type'] .'</b>';
          echo '<br />Size: <b>'. number_format($_FILES['uploadedfile']['size']/1024, 3, '.', '') .'</b> KB';
          if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
          echo '<br/><br/>File uploaded to: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b></div><br />';
        }
        else echo '<div class="style7"><b>Unable to upload the file.</b></div><br />';
      }
      else echo $err;
    }

@vibtune.scoobie Typically we want to see their code so we can tell them what's wrong, not hand them a page long of code to try/use. It may seem like helping them, but they didn't learn anything from someone giving them a bunch of code. Also, solving problems with their code helps others that may have the same issue in the future ;-)

thanks guys for your time
i just realized that this <input> tag not in my <form> tag.

<input id = "file_lead" type= "file" accept="image/*" name="file_ads_lead" style='visibility: hidden;' onchange="ChangeText(this, 'txt_lead');"/>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.