簡體   English   中英

php文件未通過ajax請求上傳

[英]Php file is not uploading with ajax request

這是我的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行的值計數不匹配

undefined index錯誤消息的原因是因為您輸入的名稱不匹配。

你有這個:

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

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

將其重命名為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM