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."

    <?php


        if (isset($result)) {
          echo "<p><strong>$result</strong></p>";
        }
        ?>
        <form action="" method="post" enctype="multipart/form-data">
          <div class="contentAdsForm">
          <div class="ContactAdsInfo">

          <table width="600" border="0" cellpadding="5" cellspacing="0" id="infoContact">
          <tr>
            <td colspan="4" align="right" class="blueBigHeaders">بيانات الاتصال</td>
            </tr>
          <tr>
            <td align="right"><input type="text" name="mobile" id="mobile" /></td>
            <td class="blueSmallHeaders">رقم الموبيل</td>
            <td align="right"><label for="mobile"></label>
              <input type="text" name="name" id="name" /></td>
            <td class="blueSmallHeaders">الاسم</td>
          </tr>
          <tr>
            <td align="right"><input type="text" name="landPhone" id="landPhone" /></td>
            <td class="blueSmallHeaders">رقم الأرضي</td>
            <td align="right"><input type="text" name="email" id="email" /></td>
            <td class="blueSmallHeaders">البريد الإلكتروني</td>
          </tr>
          </table>

          </div>
          <div class="ContactAdsInfo">
          <table width="600" border="0" cellpadding="5" cellspacing="0" id="infoAds">
          <tr>
            <td colspan="4" align="right"><span class="blueBigHeaders">بيانات الإعلان</span></td>
            </tr>
          <tr>
            <td colspan="3" align="right"><input type="text" name="adsSubject" id="adsSubject" /></td>
            <td width="86" align="right" class="blueSmallHeaders">عنوان الإعلان</td>
          </tr>
          <tr>
            <td colspan="3" align="right"><label for="adsDetails"></label>
              <textarea name="adsDetails" id="adsDetails" cols="45" rows="5"></textarea></td>
            <td align="right" valign="top" class="blueSmallHeaders">تفاصيل الإعلان</td>
          </tr>
          <tr>
            <td width="218" align="right"><label for="adsImage"></label>
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo 'MAX_FILE_SIZE'; ?>" />
              <input type="file" name="image" id="image" /></td>
            <td width="62" align="right" class="blueSmallHeaders">صورة</td>
            <td width="194" align="right"><input type="text" name="price" id="price" /></td>
            <td align="right" class="blueSmallHeaders">الثمن</td>
          </tr>
        </table>
        <table width="600" border="0" cellpadding="10">
          <tr>
            <td align="center"><input type="submit" name="submitAds" id="submitAds" value="أضف الإعلان" /></td>
          </tr>
        </table>

          </div>
          </div>
        </form>

        <?php
        if(isset($_GET['catid'])){
            $catid=$_GET['catid'];
        }
        if(!isset($_GET['catid']) || empty($_GET['catid'])){
            header("Location:index.php?pageId=3");
        }
        if(isset($_GET['kind'])){
            $kind=$_GET['kind'];
        }
        if(!isset($_GET['kind']) || empty($_GET['kind'])){
            header("Location:index.php?pageId=3");
        }
            if(isset($_POST['submitAds'])){
            $name=$_POST['name'];
            $email=$_POST['email'];
            $mobile=$_POST['mobile'];
            $landPhone=$_POST['landPhone'];
            $adsSubjuct=$_POST['adsSubject'];
            $adsDetails=$_POST['adsDetails'];
            $price=$_POST['price'];


                $putData="INSERT INTO ads(id, kind, cat, name, email, mobile, landPhone, ads_tiltle, use_price, ads_description, dateAdd)VALUES
                ('', '$catid', '$kind', '$name', '$email', '$mobile', '$landPhone', '$adsSubjuct', '$price', '$adsDetails', NOW())";
                $QputData=$db->query($putData)or die("$db->error");
                $ADSID=$db->insert_id;

            define('MAX_FILE_SIZE', 1024 * 50);
            if(array_key_exists('submitAds', $_POST)){
                define('UPLOAD_DIR', 'includes/adsImages/');
                $file=str_replace(' ', '_', $_FILES['image']['name']);
                $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg','image/png');
        if (in_array($_FILES['image']['type'], $permitted)
              && $_FILES['image']['size'] > 0 
              && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
            switch($_FILES['image']['error']) {
              case 0:
              // check if a file of the same name has been uploaded
                if (!file_exists(UPLOAD_DIR . $file)) {
              // move the file to the upload folder and rename it
                  $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .$file);
                }else {
                  $result = 'A file of the same name already exists.';
                }
                if($success){
            //here what you need yousef
            $updateADSImage = "UPDATE ads SET image='".$file."' WHERE id='".$ADSID."'";
            $updateADSImageResults= $db->query($updateADSImage) or die("$db->error");
                  $result="$file uploaded successfully.";
                }else{
                $result="Error uploading $file. Please try again.";
                }
              break;
              case 3:
              case 6:
              case 7:
              case 8:
                $result = "Error uploading $file. Please try again.";
                break;
              case 4:
                $result = "You didn't select a file to be uploaded.";
            }
          } else {
            $result = "$file is either too big or not an image.";
          }

                }
                if($QputData){
                echo "OKKKKKKKKK";
                }else{
                    echo"NOOOOOOO";
                }
            }
        ?>

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

@johnef_sh

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.

The error means that your message is undefined:

The reason is here:

$result = 'A file of the same name already exists.';

On top of your code you have this:

if (isset($result)) {
echo "<p><strong>$result</strong></p>";
}

Do you see that you have 2 different messages and functions.

The solution is create a variable for that message.

$result=$_POST['result'];

2- the same for this big file size it don't give proper message as "$file is either too big or not an image."

Well, it's the same thing as the first issue you have above.

hay LastMitch I did but didn't solve the problem can you please make it more clear for me
I added the
$result=$_POST['result'];
but I got notes undefine $result can you please tell me what to do here.
Thanks and regards.

Member Avatar for LastMitch

@johnef_sh

I did but didn't solve the problem can you please make it more clear for me

Did you write this code? I assume you know why it's undefine $result because if you did something like this already:

if(isset($_POST['submitAds'])){
$name=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$landPhone=$_POST['landPhone'];
$adsSubjuct=$_POST['adsSubject'];
$adsDetails=$_POST['adsDetails'];
$price=$_POST['price'];

This:

$result=$_POST['result'];

Should be in here:

if(isset($_POST['result'])) {
$result=$_POST['result'];
}

Does that make sense?

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.