473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2581
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

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

Similar topics

0
by: Don Stevic | last post by:
I am running VS2005 and using VSTO for Word. I keep getting this error saying that there is no source code available for this location when I try and run this application. I am going to...
6
by: genc ymeri | last post by:
Hi, We are struggeling to upload a file through a C# webClient into JBoss web server. Meanwhile we are able to upload a file from the webserver itself. The problem is only with C# webClient . The...
1
by: henry | last post by:
Hi all, When user upload a file, there is a size limit (default is 4M). And we can change this size limit by changging the config file. If a user upload a file bigger than this size, an DNS...
1
by: Jeremy Ames | last post by:
I have a datagrid that updates the table using a stored procedure. The stored procedure is confirmed to complete correctly, yet the sql data adapter is returning an error that my application is...
3
by: Gavin | last post by:
I need some help, I need the code to allow people that visit my website to be able to upload pictures to a file on my web server. I have been able to get close, but not quite there yet. Set fso...
1
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
3
by: meenu_susi | last post by:
hi... i have an option for the users to upload their image... but i want to allow only gif and jpg file extension.... how to check whether the uploaded image is gif or jpg,, and display...
2
by: LEX | last post by:
Could not update; currently locked by another session on this machine. Can someone help me figure out why access gives me this error on some record's memo fields but not on others?
2
by: Ming | last post by:
Hi there, Is there a handy way to show error message instead of error code when uploading? For example, if I get error code 3 (UPLOAD_ERR_PARTIAL), I can directly show users that 'Only part...
3
anfetienne
by: anfetienne | last post by:
Hi all, this is real simple, I've written this code so I can duplicate images/files after uploading it with jumploader (this doesn't allow for duplicates during the upload process like standard...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.