469,076 Members | 1,915 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

uploading image (same name) ignores error message, shows "Notice: Undefined variable:

hay I have this code to upload images it works OK bu there is 2 things not working
1- when the image has the same name it doesn't display the message "A file of the same name already exists" it just give me this Notice "Notice: Undefined variable: success in C:\wamp\www\joon\xxxxx\xxxxx\xxxxx.php on line 108" why.
2- the same for this big file size it don't give proper message as "$file is either too big or not an image."

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4.         if (isset($result)) {
  5.           echo "<p><strong>$result</strong></p>";
  6.         }
  7.         ?>
  8.         <form action="" method="post" enctype="multipart/form-data">
  9.           <div class="contentAdsForm">
  10.           <div class="ContactAdsInfo">
  11.  
  12.           <table width="600" border="0" cellpadding="5" cellspacing="0" id="infoContact">
  13.           <tr>
  14.             <td colspan="4" align="right" class="blueBigHeaders">بيانات الاتصال</td>
  15.             </tr>
  16.           <tr>
  17.             <td align="right"><input type="text" name="mobile" id="mobile" /></td>
  18.             <td class="blueSmallHeaders">رقم الموبيل</td>
  19.             <td align="right"><label for="mobile"></label>
  20.               <input type="text" name="name" id="name" /></td>
  21.             <td class="blueSmallHeaders">الاسم</td>
  22.           </tr>
  23.           <tr>
  24.             <td align="right"><input type="text" name="landPhone" id="landPhone" /></td>
  25.             <td class="blueSmallHeaders">رقم الأرضي</td>
  26.             <td align="right"><input type="text" name="email" id="email" /></td>
  27.             <td class="blueSmallHeaders">البريد الإلكتروني</td>
  28.           </tr>
  29.           </table>
  30.  
  31.           </div>
  32.           <div class="ContactAdsInfo">
  33.           <table width="600" border="0" cellpadding="5" cellspacing="0" id="infoAds">
  34.           <tr>
  35.             <td colspan="4" align="right"><span class="blueBigHeaders">بيانات الإعلان</span></td>
  36.             </tr>
  37.           <tr>
  38.             <td colspan="3" align="right"><input type="text" name="adsSubject" id="adsSubject" /></td>
  39.             <td width="86" align="right" class="blueSmallHeaders">عنوان الإعلان</td>
  40.           </tr>
  41.           <tr>
  42.             <td colspan="3" align="right"><label for="adsDetails"></label>
  43.               <textarea name="adsDetails" id="adsDetails" cols="45" rows="5"></textarea></td>
  44.             <td align="right" valign="top" class="blueSmallHeaders">تفاصيل الإعلان</td>
  45.           </tr>
  46.           <tr>
  47.             <td width="218" align="right"><label for="adsImage"></label>
  48.             <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo 'MAX_FILE_SIZE'; ?>" />
  49.               <input type="file" name="image" id="image" /></td>
  50.             <td width="62" align="right" class="blueSmallHeaders">صورة</td>
  51.             <td width="194" align="right"><input type="text" name="price" id="price" /></td>
  52.             <td align="right" class="blueSmallHeaders">الثمن</td>
  53.           </tr>
  54.         </table>
  55.         <table width="600" border="0" cellpadding="10">
  56.           <tr>
  57.             <td align="center"><input type="submit" name="submitAds" id="submitAds" value="أضف الإعلان" /></td>
  58.           </tr>
  59.         </table>
  60.  
  61.           </div>
  62.           </div>
  63.         </form>
  64.  
  65.         <?php
  66.         if(isset($_GET['catid'])){
  67.             $catid=$_GET['catid'];
  68.         }
  69.         if(!isset($_GET['catid']) || empty($_GET['catid'])){
  70.             header("Location:index.php?pageId=3");
  71.         }
  72.         if(isset($_GET['kind'])){
  73.             $kind=$_GET['kind'];
  74.         }
  75.         if(!isset($_GET['kind']) || empty($_GET['kind'])){
  76.             header("Location:index.php?pageId=3");
  77.         }
  78.             if(isset($_POST['submitAds'])){
  79.             $name=$_POST['name'];
  80.             $email=$_POST['email'];
  81.             $mobile=$_POST['mobile'];
  82.             $landPhone=$_POST['landPhone'];
  83.             $adsSubjuct=$_POST['adsSubject'];
  84.             $adsDetails=$_POST['adsDetails'];
  85.             $price=$_POST['price'];
  86.  
  87.  
  88.                 $putData="INSERT INTO ads(id, kind, cat, name, email, mobile, landPhone, ads_tiltle, use_price, ads_description, dateAdd)VALUES
  89.                 ('', '$catid', '$kind', '$name', '$email', '$mobile', '$landPhone', '$adsSubjuct', '$price', '$adsDetails', NOW())";
  90.                 $QputData=$db->query($putData)or die("$db->error");
  91.                 $ADSID=$db->insert_id;
  92.  
  93.             define('MAX_FILE_SIZE', 1024 * 50);
  94.             if(array_key_exists('submitAds', $_POST)){
  95.                 define('UPLOAD_DIR', 'includes/adsImages/');
  96.                 $file=str_replace(' ', '_', $_FILES['image']['name']);
  97.                 $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg','image/png');
  98.         if (in_array($_FILES['image']['type'], $permitted)
  99.               && $_FILES['image']['size'] > 0 
  100.               && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
  101.             switch($_FILES['image']['error']) {
  102.               case 0:
  103.               // check if a file of the same name has been uploaded
  104.                 if (!file_exists(UPLOAD_DIR . $file)) {
  105.               // move the file to the upload folder and rename it
  106.                   $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .$file);
  107.                 }else {
  108.                   $result = 'A file of the same name already exists.';
  109.                 }
  110.                 if($success){
  111.             //here what you need yousef
  112.             $updateADSImage = "UPDATE ads SET image='".$file."' WHERE id='".$ADSID."'";
  113.             $updateADSImageResults= $db->query($updateADSImage) or die("$db->error");
  114.                   $result="$file uploaded successfully.";
  115.                 }else{
  116.                 $result="Error uploading $file. Please try again.";
  117.                 }
  118.               break;
  119.               case 3:
  120.               case 6:
  121.               case 7:
  122.               case 8:
  123.                 $result = "Error uploading $file. Please try again.";
  124.                 break;
  125.               case 4:
  126.                 $result = "You didn't select a file to be uploaded.";
  127.             }
  128.           } else {
  129.             $result = "$file is either too big or not an image.";
  130.           }
  131.  
  132.                 }
  133.                 if($QputData){
  134.                 echo "OKKKKKKKKK";
  135.                 }else{
  136.                     echo"NOOOOOOO";
  137.                 }
  138.             }
  139.         ?>
Jan 20 '13 #1
3 2394
acoder
16,027 Expert Mod 8TB
If the condition on line 104 is false, $success is never set (which you're checking on line 110). Move that portion of your code to after line 106.
Jan 20 '13 #2
ya I did and now I don't get the notes but also I don't get my message that the file of the same name has been uploaded can you please write it down for me how it can be... and also I don't get the other validations like the file size please check my code again...
Thanks
Jan 21 '13 #3
acoder
16,027 Expert Mod 8TB
Which notes?

You're not going to get any message if you display $result at the top and the code to upload the file appears at the bottom. This code needs to move to the top of the page.
Jan 21 '13 #4

Post your reply

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

Similar topics

reply views Thread by Don Stevic | last post: by
1 post views Thread by Jeremy Ames | last post: by
3 posts views Thread by Gavin | last post: by
2 posts views Thread by LEX | last post: by
2 posts views Thread by Ming | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.