-1

I have a problem to fix this type error, I am also declare the variable the its given a problem.

This is my complete file:

$success='';
$error1='';
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');

$max_file_size = 2000* 1024; #200kb

$nw = $nh = 50;
# image with # height

if(isset($_REQUEST['submit']))  
{
    $title=$_REQUEST['title'];
    $date=date('d-M-Y');
    /****prod image*******************************/
    $fn1=$_FILES['image']['name'];

    $fs1=$_FILES['image']['size'];

    $ftemp1=$_FILES['image']['tmp_name'];

    $img_type1=$_FILES['image']['type'];

    if(! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));

        if (in_array($ext, $valid_exts))
        {
            // Check if file already exists
            $path = 'image/thumb/'.$fn1;
            $size = getimagesize($_FILES['image']['tmp_name']);

            $x = (int) $_POST['x'];
            $y = (int) $_POST['y'];
            $w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
            $h = (int) $_POST['h'] ? $_POST['h'] : $size[1];

            $data = file_get_contents($_FILES['image']['tmp_name']);
            $vImg = imagecreatefromstring($data);
            $dstImg = imagecreatetruecolor($nw, $nh);
            imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
            imagejpeg($dstImg, $path);
            imagedestroy($dstImg);

            if(move_uploaded_file ($ftemp1,"image/$fn1")){
                $result="INSERT INTO add_banner VALUES ('','$title','$fn1','$date')";
                if(mysql_query($result)){

                    //echo "<script>alert('Banner Added Successfully !');</script>";
                    $success='<div class="alert alert-success" role="alert"><strong>Well done!</strong> You successfully add the banner.</div>';

                    // echo "<script>document.location.href='front_banner.php'</script>";
                } else {
                    $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Change a few things up and try submitting again.</div>';
                    //echo "<script>document.location.href='front_banner.php'</script>";
                }
            } else {
                $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Change a few things up and try submitting again.</div>';
                //echo "<script>document.location.href='front_banner.php'</script>";
            }
        } else {
            $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>';         
        }
    } else {
        $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Sorry, Please fill the Field</div>';
    }
}

This is my output:


Notice: Undefined index: x in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 28

Notice: Undefined index: y in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 29

Notice: Undefined index: w in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 30

Notice
: Undefined index: h in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 31

What am I doing wrong?

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45

6 Answers6

0
<?php if(isset($success)) { echo $success ; } .''.if(isset($error1)) { echo $error1 ;}.''.if(isset($error)) { echo $error;}?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0

You can initialize the variables in your php-file where you set it.

Something like this:

<?php
$success = "";
$error1 = "";
$error = "";

Then you can be sure that your vars are not undefined and you can use them without this "undefined error".

After these lines, you can do whatever you wanna do.

EDIT

Looks like your $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'] is not set. Try to var_dump($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']) to see if it's filled.

You could check if this is set with:

if ($_POST['x'] != "" && $_POST['y'] != "" && $_POST['w'] != "" && $_POST['h'] != "") {

or

if (!empty($_POST['x']) && !empty($_POST['y']) && !empty($_POST['w']) && !empty($_POST['h'])) {

or

if (isset($_POST['x']) && isset($_POST['y']) && isset($_POST['w']) && isset($_POST['h'])) {

    $x = (int) $_POST['x'];
    $y = (int) $_POST['y'];
    $w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
    $h = (int) $_POST['h'] ? $_POST['h'] : $size[1];

    // do your thing ...
} else {
    $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Blablabla </div>';         
}

EDIT 2

At the point where you ask if if (in_array($ext, $valid_exts)), add the code from my first EDIT below. This could look like this:

if (in_array($ext, $valid_exts)) {
    if ($_POST['x'] != "" && $_POST['y'] != "" && $_POST['w'] != "" && $_POST['h'] != "") {

        // ...
        // here is your code where you set $x, $y, $w, $h and the other things
        // ...

    } else {
        $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Blablabla </div>';         
    }
} else {
    $error1='<div class="alert alert-danger" role="alert"><strong>Oh snap!</strong> Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>';         
}

Hope this helps.

Patrick Mlr
  • 2,955
  • 2
  • 16
  • 25
  • Notice: Undefined index: x in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 28

    Notice: Undefined index: y in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 29

    Notice: Undefined index: w in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 30

    Notice: Undefined index: h in E:\xampp\htdocs\novus_admin_panel\web\front_banner.php on line 31
    – Abhishek Shukla Sep 13 '16 at 07:32
  • i am not get the solution can you provide a full solution – Abhishek Shukla Sep 13 '16 at 08:04
0

I don't understand exactly your problem but this code looks better to show error's message.

<?php
if(isset($success) && !empty($success))
{
    echo $success." ";
}
if(isset($error1) && !empty($success))
{
    echo $success." ";
}
if(isset($success) && !empty($success))
{
    echo $success;
}
?>
0

Use isset http://php.net/manual/en/function.isset.php

<?php
$success = "";
$error1 = "";
$error = "";

if (isset($success)) echo $success;
if (isset($error1)) echo $error1;
if (isset($error)) echo $error;
?>
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
0

The $_POST['x'] value and other for indexes y, w and h are not defined. So you have to check it before use it.

For example, try

$x = (isset($_POST['x'])? (int) $_POST['x'] : 0;

Do the same for $_POST['y'], and for h and w you'd better write something as following (I used if condition for better readability):

if (isset($_POST['h']) && ((int) $_POST['h'] > 0)) {
    $h = (int) $_POST['h'] ;
} else {
    $h = $size['h'] ;
}

Of course, check the submition of your POST too, as it seems it does not defined these values.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
0

Try this:

$x = (int) ( isset($_POST['x']) ? $_POST['x'] : 0 );
$y = (int) ( isset($_POST['y']) ? $_POST['y'] : 0 );
$w = (int) ( isset($_POST['w']) ? $_POST['w'] : $size[0] );
$h = (int) ( isset($_POST['h']) ? $_POST['h'] : $size[1] );
Marc Compte
  • 4,579
  • 2
  • 16
  • 22
  • I have got this error
    Notice: getimagesize(): Read error! in E:\xampp\htdocs\novus_admin_panel\web\article_add.php on line 50

    Warning: imagecreatefromstring(): Empty string or invalid image in E:\xampp\htdocs\novus_admin_panel\web\article_add.php on line 58

    Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in E:\xampp\htdocs\novus_admin_panel\web\article_add.php on line 60
    – Abhishek Shukla Sep 14 '16 at 12:19
  • The `Read error!` may happen if the file did not upload correctly, or if you don't specify the proper full path to the file or if the file has 0 bytes. http://php.net/manual/en/function.getimagesize.php – Marc Compte Sep 15 '16 at 13:42