Upload file with specific dimensions

  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

I searched a bit for this on google, but couldn't find anything. Basically I want to restrict an image file upload in respect to it's dimension. For example, if i upload a 40x40 image i would like to set the accepted dimensions only for 728x90 so that it could go into the appropriate directory. Here is the code:

PHP Code: [ Select ]
<form enctype="multipart/form-data" action="" method="POST">
<select name="select" class="select">
<option value="">Site:
<?php
$dirPath = dir('./');
 
while (($file = $dirPath->read()) !== false)
{
if($file == "." || $file == ".." || $file == "img" || eregi("\.php",$file) || eregi("\.jpg",$file) || eregi("\.css",$file) )
        continue;
   echo "<option value=\"" . trim($file) . "\">" . $file . "\n";
}
$dirPath->close();
?>
</select>
<select name="rez" class="select">
  <option value="">Rezolutie:</option>
  <option value="abc">728x90</option>
  <option value="def">468x60</option>
  <option value="ghj">234x60</option>
</select>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br />
Selecteaza fisier: <input name="uploaded_file" type="file" />
<input type="submit" value="" id="ok" class="browse" />
</form>
 
<?php
$select=$_POST['select'];
$rez=$_POST['rez'];
 
// Where the file is going to be placed
$target_path = $select.'/';
 
//?heck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
    ($_FILES["uploaded_file"]["size"] < 1000000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/'.$select.'/'.$rez.'/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "<u>Reclama a fost atasata in Website-ul</u>: ".$select."<br /><br /><center><img src=\"http://localhost/demo/".$select.'/'.$rez.'/'.$filename."\" id=\"add_img\" width=\"50%\" /></center> ";
        } else {
           echo "Eroare: <b class=\"err\">Reclama nu a putut fi atasata!</b>";
        }
      } else {
         echo "Eroare: <b class=\"err\">Reclama existenta!</b>";
      }
  } else {
     echo "Eroare: <b class=\"err\">Doar reclame cu format .jpg cu marimea sub 1MB sunt acceptate!</b>";
  }
} else {
 echo "";
}
 
?>
 
 
  1. <form enctype="multipart/form-data" action="" method="POST">
  2. <select name="select" class="select">
  3. <option value="">Site:
  4. <?php
  5. $dirPath = dir('./');
  6.  
  7. while (($file = $dirPath->read()) !== false)
  8. {
  9. if($file == "." || $file == ".." || $file == "img" || eregi("\.php",$file) || eregi("\.jpg",$file) || eregi("\.css",$file) )
  10.         continue;
  11.    echo "<option value=\"" . trim($file) . "\">" . $file . "\n";
  12. }
  13. $dirPath->close();
  14. ?>
  15. </select>
  16. <select name="rez" class="select">
  17.   <option value="">Rezolutie:</option>
  18.   <option value="abc">728x90</option>
  19.   <option value="def">468x60</option>
  20.   <option value="ghj">234x60</option>
  21. </select>
  22. <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br />
  23. Selecteaza fisier: <input name="uploaded_file" type="file" />
  24. <input type="submit" value="" id="ok" class="browse" />
  25. </form>
  26.  
  27. <?php
  28. $select=$_POST['select'];
  29. $rez=$_POST['rez'];
  30.  
  31. // Where the file is going to be placed
  32. $target_path = $select.'/';
  33.  
  34. //?heck that we have a file
  35. if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  36.   //Check if the file is JPEG image and it's size is less than 350Kb
  37.   $filename = basename($_FILES['uploaded_file']['name']);
  38.   $ext = substr($filename, strrpos($filename, '.') + 1);
  39.   if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
  40.     ($_FILES["uploaded_file"]["size"] < 1000000)) {
  41.     //Determine the path to which we want to save this file
  42.       $newname = dirname(__FILE__).'/'.$select.'/'.$rez.'/'.$filename;
  43.       //Check if the file with the same name is already exists on the server
  44.       if (!file_exists($newname)) {
  45.         //Attempt to move the uploaded file to it's new place
  46.         if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
  47.            echo "<u>Reclama a fost atasata in Website-ul</u>: ".$select."<br /><br /><center><img src=\"http://localhost/demo/".$select.'/'.$rez.'/'.$filename."\" id=\"add_img\" width=\"50%\" /></center> ";
  48.         } else {
  49.            echo "Eroare: <b class=\"err\">Reclama nu a putut fi atasata!</b>";
  50.         }
  51.       } else {
  52.          echo "Eroare: <b class=\"err\">Reclama existenta!</b>";
  53.       }
  54.   } else {
  55.      echo "Eroare: <b class=\"err\">Doar reclame cu format .jpg cu marimea sub 1MB sunt acceptate!</b>";
  56.   }
  57. } else {
  58.  echo "";
  59. }
  60.  
  61. ?>
  62.  
  63.  


I would rely appreciate some advice on this :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • dazraf
  • Born
  • Born
  • dazraf
  • Posts: 2

Post 3+ Months Ago

So does your code works or what...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

The getimagesize function will get you the dimensions and type of the image along with some other information.

I would use it somewhere around this line to determine what I should do with the image.

Code: [ Select ]
$newname = dirname(__FILE__).'/'.$select.'/'.$rez.'/'.$filename;


I've not seen any way to determine or restrict file uploads based on image dimensions in the browser side.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.