简体   繁体   中英

Php file is not uploading with ajax request

this is my html form which is validated by php with ajax request. But after submit it's showing me following error:

Notice: Undefined index: img in D:\Software Installed\xampp\htdocs\wix\users\insert_galary.php on line 12

if i delete the file portion of the form then it's working fine and also if i check it

if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload galary")

then it's not validate my data.

Html form

<p><a href="javascript:void(null);" onclick="showDialog2();">Add more</a></p>                
            <div id="dialog-modal2" title="Upload your galary image" style="display: none;">

            <form method="post" action="insert_galary.php" id="myForm2" enctype="multipart/form-data" >

            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td>Image title</td>
            <td><input type="text" name="g_title" placeholder="Image title" class="tr" /></td>
          </tr>                
          <tr>
            <td>Upload image</td>
            <td><input type="file" name="file" class="tr"/></td>
          </tr>
          <tr>
            <td>Image link</td>
            <td><input type="text" name="g_link"  placeholder="Image link"  class="tr"/> <span style="color:#030;">* must have http://</span>
            </td>
          </tr> 
            <tr>
                <td><input type="submit" id="sub2" value="Upload Galary" name="Submit" class="submit"/></td>
              </tr>
            </table>
            </form> 

            <span id="result2"></span>


            <script type="text/javascript">
            $("#sub2").click( function() {
             $.post( $("#myForm2").attr("action"), 
                     $("#myForm2 :input").serializeArray(), 
                     function(info){ $("#result2").html(info); 
               });
             clearInput();
            });

            $("#myForm2").submit( function() {
              return false; 
            });

            </script>
            </div>


function showDialog2()
{
$("#dialog-modal2").dialog(
{
    width: 610,
    height: 350,
    open: function(event, ui)
    {
        var textarea = $('<textarea style="height: 276px;">');
        $(textarea).redactor({
            focus: true,
            autoresize: false,
            initCallback: function()
            {
                this.set('<p>Lorem...</p>');
            }
        });
    }
 });
}

Php code: (insert_galary.php)

        if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload Galary")
        {
            $current_date = date('Y-m-d H:i:s');    

            $g_title =  inputvalid($_POST['g_title']);  
            $g_link =  inputvalid($_POST['g_link']);


            $gid = mt_rand(100000, 999999); 
            $file =  $_FILES["file"]["name"];
            $type =  $_FILES["file"]["type"];
            $size =  ($_FILES["file"]["size"] / 1024);
            $temp =  $_FILES["file"]["tmp_name"];   
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);    
            $galary_pic = $gid.".".$extension;  
            $galary_directory = "../galary_images/";

            $err =  array();

            if(isset($g_link) && isset($g_title) && isset($file))
            {
                if(empty($g_link) && empty($g_title) && empty($file))
                    $err[] = "All field require";
                else
                {
                    if(empty($g_title)) 
                        $err[] = "blog title require";
                    elseif(strlen($g_title) > 250 )
                        $err[] = "blog title is too long";
                    elseif(strlen($g_title) < 2 )
                        $err[] = "blog title is too small"; 

                    if(strlen($g_link) > 250)
                        $err[] = "image link is too long";


                    if(empty($file))                    
                        $err[] = "Upload your picture";             
                    elseif(!in_array($extension, $allowedExts))
                        $err[] = "Uploaded file must be gif, jpeg, jpg, png format";
                    elseif($size > 500)
                        $err[] = "Uploaded file must be within 500kb";                  


                }
            }

            if(!empty($err))
            {
                echo "<div class='error'>"; 
                foreach($err as $er)
                {
                    echo "<font color=red>$er.</font><br/>";                
                }
                echo "</div>";
                echo "<br/>";
            }
            else
            {


                if($insert = mysql_query("INSERT INTO galary VALUES('', '', '', '', '$username', '$g_title', '', '$galary_pic', '$g_link', '$current_date', '')"))              
                {




                define('DESIRED_IMAGE_WIDTH', 338);
                define('DESIRED_IMAGE_HEIGHT', 338);            


            $source_path = $_FILES['file']['tmp_name'];
            list($source_width, $source_height, $source_type) = getimagesize($source_path);

            switch ($source_type) {
                case IMAGETYPE_GIF:
                    $source_gdim = imagecreatefromgif($source_path);
                    break;
                case IMAGETYPE_JPEG:
                    $source_gdim = imagecreatefromjpeg($source_path);
                    break;
                case IMAGETYPE_PNG:
                    $source_gdim = imagecreatefrompng($source_path);
                    break;
            }

            $source_aspect_ratio = $source_width / $source_height;
            $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

            if ($source_aspect_ratio > $desired_aspect_ratio) {
                $temp_height = DESIRED_IMAGE_HEIGHT;
                $temp_width = ( int ) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
            } else {
                $temp_width = DESIRED_IMAGE_WIDTH;
                $temp_height = ( int ) (DESIRED_IMAGE_WIDTH / $source_aspect_ratio);
            }

            $temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
            imagecopyresampled(
                $temp_gdim,
                $source_gdim,
                0, 0,
                0, 0,
                $temp_width, $temp_height,
                $source_width, $source_height
            );

            $x0 = ($temp_width - DESIRED_IMAGE_WIDTH) / 2;
            $y0 = ($temp_height - DESIRED_IMAGE_HEIGHT) / 2;
            $desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
            imagecopy(
                $desired_gdim,
                $temp_gdim,
                0, 0,
                $x0, $y0,
                DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
            );
            $desired_gdim;

            //$file = $_FILES["file"]["name"];
            //header('Content-type: image/jpeg');
            //imagejpeg($desired_gdim, $file);

            //imagejpeg($desired_gdim, $source_path);
            imagejpeg( $desired_gdim, "$upload_directory/$galary_pic" );
            //header("Location: img/");





                echo "<div class=success>";
                echo "successfully uploaded new image.";
                echo "</div>";
                header("Refresh:3, url=all_galary.php");
                }
                else
                {
                    echo "Sorry, I can't upload your galary";   
                    echo mysql_error();
                }

            }
        }
        ?>

Can anyone tell me why it's showing me the Error Message ?

Updated Error Message :

Notice: Undefined index: file in D:\\Software Installed\\xampp\\htdocs\\wix\\users\\insert_galary.php on line 16

Notice: Undefined index: file in D:\\Software Installed\\xampp\\htdocs\\wix\\users\\insert_galary.php on line 17

Notice: Undefined index: file in D:\\Software Installed\\xampp\\htdocs\\wix\\users\\insert_galary.php on line 18

Notice: Undefined index: file in D:\\Software Installed\\xampp\\htdocs\\wix\\users\\insert_galary.php on line 19

Notice: Undefined index: file in D:\\Software Installed\\xampp\\htdocs\\wix\\users\\insert_galary.php on line 21 Sorry, I can't upload your galaryColumn count doesn't match value count at row 1

The reason for the undefined index error message is because your input name doesn't match.

You have this:

<input type="file" name="img" class="tr"/>

and you're using $file = $_FILES["file"]["name"];

Rename it to:

<input type="file" name="file" class="tr"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM