By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Image upload verification

P: 3
hi, i write this code for image uploading in 3 size and it works properly. but when i dont select any file and click the upload button , its show the message
"Image Uploaded Sucessfully"...
thanks in advance

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4.  define ("MAX_SIZE","50");
  5.  function getExtension($str) {
  6.          $i = strrpos($str,".");
  7.          if (!$i) { return ""; }
  8.          $l = strlen($str) - $i;
  9.          $ext = substr($str,$i+1,$l);
  10.          return $ext;
  11.  }
  12.  
  13.  $errors=0;
  14.  
  15.  if(isset($_POST))
  16.  {
  17.  
  18.      $image =$_FILES["u_image"]["name"];
  19.     $uploadedfile = $_FILES['u_image']['tmp_name'];
  20.  
  21.  
  22.      if ($image)
  23.      {
  24.  
  25.          $filename = stripslashes($_FILES['u_image']['name']);
  26.  
  27.           $extension = getExtension($filename);
  28.          $extension = strtolower($extension);
  29.  
  30.  
  31.  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  32.          {
  33.  
  34.              echo "<h1>Unknown Image extension</h1>";
  35.              $errors=1;
  36.  
  37.          }
  38.          else
  39.          {
  40.  
  41.  
  42.  $size=filesize($_FILES['u_image']['tmp_name']);
  43.  
  44. if ($size > MAX_SIZE*1024)
  45. {
  46.     echo "<h1>You have exceeded the size limit!</h1> ";
  47.     $errors=1;
  48.  
  49.  
  50. }
  51.  
  52. else{
  53.  
  54. if($extension=="jpg" || $extension=="jpeg" )
  55. {
  56. $uploadedfile = $_FILES['u_image']['tmp_name'];
  57. $src = imagecreatefromjpeg($uploadedfile);
  58.  
  59. }
  60. else if($extension=="png")
  61. {
  62. $uploadedfile = $_FILES['u_image']['tmp_name'];
  63. $src = imagecreatefrompng($uploadedfile);
  64.  
  65. }
  66. else
  67. {
  68. $src = imagecreatefromgif($uploadedfile);
  69. }
  70.  
  71. echo $scr;
  72.  
  73. list($width,$height)=getimagesize($uploadedfile);
  74.  
  75.  
  76. $newwidth=150;
  77. $newheight=150;  //($height/$width)*$newwidth;
  78. $tmp=imagecreatetruecolor($newwidth,$newheight);
  79.  
  80.  
  81. $newwidth1=50;
  82. $newheight1=50;  //($height/$width)*$newwidth1;
  83. $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
  84.  
  85. $newwidth2=$width;
  86. $newheight2=$height;
  87. $tmp2=imagecreatetruecolor($newwidth2,$newheight2);
  88.  
  89.  
  90. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  91.  
  92. imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
  93.  
  94. imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);
  95.  
  96. $filename = "http://bytes.com/images/med/".rand(). $_FILES['u_image']['name'];
  97.  
  98. $filename1 = "http://bytes.com/images/small/".time(). $_FILES['u_image']['name'];
  99.  
  100. $filename2 = "http://bytes.com/images/org/". $_FILES['u_image']['name'];
  101.  
  102.  
  103. imagejpeg($tmp,$filename,100);
  104.  
  105. imagejpeg($tmp1,$filename1,100);
  106.  
  107. imagejpeg($tmp2,$filename2,100);
  108.  
  109. imagedestroy($src);
  110. imagedestroy($tmp);
  111. imagedestroy($tmp1);
  112. imagedestroy($tmp2);
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  }
  119.  
  120. //If no errors registred, print the success message
  121.  if(isset($_POST['Submit']) && !$errors)
  122.  {
  123.  
  124.    // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
  125.      echo "<h2>Image Uploaded Successfully</h2>";
  126.  }
  127.  
  128. ?>
  129.  
  130.  
  131. <html><head>
  132.  
  133. <title>picture demo</title>
  134.  
  135.  
  136.  
  137.  
  138.   <style type="text/css">
  139.  
  140. .msgdiv{
  141.     width:759px;
  142. padding-top:8px;
  143. padding-bottom:8px;
  144. background-color: #fff;
  145. font-weight:bold;
  146. font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px;
  147. }
  148.  
  149. </style>
  150.  
  151.   </head><body>
  152.  
  153.   <form method="post" action="" enctype="multipart/form-data" name="form1">
  154. <table width="30%" border="1" align="center" cellpadding="0" cellspacing="0">
  155.  
  156.         <tr>
  157.           <td >Picture
  158.             : </td>
  159.           <td>
  160.  
  161.               <input name="u_image" type="file" id="u_image"/>
  162.  
  163.              </td>
  164.                 </tr>
  165.         <tr>
  166.             <td colspan="2" align="center">
  167.         <input type="submit" value="Upload" name="Submit"/>
  168. </td>
  169.         </tr>
  170.       </table>
  171.                 </form>
  172.  
  173.  
  174. </body></html>
  175.  
  176.  
Oct 4 '11 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
you can use javascript to verify whether any file has been chosen or not, you also can verify using your php whether you received any file or not
Oct 4 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.