Php文件没有使用ajax请求上传

时间:2013-12-08 15:59:23

标签: php ajax

这是我的html表单,由php用ajax请求验证。但是在提交之后它会向我显示以下错误:

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

如果我删除表单的文件部分,那么它工作正常,如果我检查它

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

那么它不会验证我的数据。

Html表格

<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代码:(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();
                }

            }
        }
        ?>

有谁能告诉我为什么它会向我显示错误信息?

更新了错误消息:

注意:未定义的索引:第16行的D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件

注意:未定义的索引:第17行的D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件

注意:未定义的索引:第18行的D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件

注意:未定义的索引:第19行的D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件

注意:未定义的索引:第21行的D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件 抱歉,我无法上传您的galaryColumn计数与第1行的值计数不匹配

1 个答案:

答案 0 :(得分:0)

undefined index错误消息的原因是因为您的输入名称不匹配。

你有这个:

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

您正在使用$file = $_FILES["file"]["name"];

将其重命名为:

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