Upload form image with AJAX / PHP / MySQL

2

I'm redoing a form for a site that needs to upload an image, but I'm not able to upload and need your help. I already researched and could not find the answer ... Function:

function fBlog_Send(){
$.ajax({
    type:"GET",
    url:"ad_BL_Include2.php",
    data:{
        nCategoryID:$('#nCategoryID-Include-Blog').val(),
        sTitle:$('#sTitle-Include-Blog').val(),
        sKeywords:$('#sKeywords-Include-Blog').val(),
        sDescription:$('#sDescription-Include-Blog').val(),
        sPicture:$('#sPicture-Include-Blog').val()
    },
    success:function(data){
        $('#blog-include').html(data);
    }
});

HTML:

<?php include "include_Connection.php" ?>
<div class="blog-div-tabs">
    <form id="adm-form">
        <fieldset>
            <label class="category">
                <select id="nCategoryID-Include-Blog">
                    <?php
                    $sQuery = "SELECT * FROM tcategory";
                    $rRecord = mysqli_query($sConn, $sQuery);
                    while($rRow = mysqli_fetch_assoc($rRecord)){
                        ?><option value="<?php echo $rRow["nID"];?>"><?php echo $rRow["sDescription"];?></option><?php
                    };
                    ?>
                </select>
            </label>
            <div class="clear"></div>
            <label class="title">
                <input type="text" id="sTitle-Include-Blog" placeholder="Título">
            </label>
            <div class="clear"></div>
            <label class="keywords" >
                <input type="text" id="sKeywords-Include-Blog" placeholder="Referência">
            </label>
            <div class="clear"></div>
            <label class="description">
                <textarea id="sDescription-Include-Blog" placeholder="Descrição"></textarea>
            </label>
            <div class="clear"></div>
            <label class="picture">
                <input type="file" id="sPicture-Include-Blog" placeholder="Imagem" class="image" />
            </label>
            <div class="clear"></div>
            <center><a class="href-Link-1" data-type="submit" onclick="fBlog_Send()"><img src="img/Contact/btn-Submit.png" /><img src="img/All/arrow.png" alt=""></a></center>
        </fieldset>
    </form>
</div>

PHP:

<?php include "include_Connection.php" ?>
<?php
session_start();
$nUserID_                               = $_SESSION["SS_nUserID"];
if(isset($_GET['nCategoryID'])){
    $nCategoryID_                       = strtolower($_GET['nCategoryID']);
}else{
    $nCategoryID_                       = "";
}
if(isset($_GET['sTitle'])){
    $sTitle_                            = strtoupper($_GET['sTitle']);
}else{
    $sTitle_                            = "";
}
if(isset($_GET['sKeywords'])){
    $sKeywords_                         = strtoupper($_GET['sKeywords']);
}else{
    $sKeywords_                         = "";
}
if(isset($_GET['sDescription'])){
    $sDescription_                      = strtoupper($_GET['sDescription']);
}else{
    $sDescription_                      = "";
}
if(isset($_FILES['sPicture']['name'])){
    $sPicture_                          = $_FILES['sPicture']['name'];
}else{
    $sPicture_                          = "";
}
$dDate_                                 = date("Y-m-d");
if($_FILES['sPicture']['name'] <> ''){
    if(move_uploaded_file($_FILES['sPicture']['tmp_name'], $sPath.'Original/'.$_FILES['lURL1']['name'])){
        $sImg = imagecreatefromjpeg($sPath.'Original/'.$_FILES['sPicture']['name']);
        $nWidth = imagesx($sImg);
        $nHeight = imagesy($sImg);
        $nWidth_tmp = 480;
        $nHeight_tmp = 360;
        if($nWidth > $nWidth_tmp || $nHeight > $nHeight_tmp){
            if($nWidth < $nHeight){
                $nImageWidth = round(($nWidth * $nHeight_tmp) / $nHeight);
                $nImageHeight = $nHeight_tmp;
            }elseif($nWidth > $nHeight){
                $nImageWidth = $nWidth_tmp;
                $nImageHeight = round(($nHeight * $nWidth_tmp) / $nWidth);
            }else{
                $nImageWidth = $nWidth_tmp;
                $nImageHeight = $nHeight_tmp;
            }
        }else{
            $nImageWidth = $nWidth;
            $nImageHeight = $nHeight;
        }
        $nImg_ = imagecreatetruecolor($nImageWidth, $nImageHeight);
        imagecopyresampled($nImg_, $sImg, 0, 0, 0, 0, $nImageWidth, $nImageHeight, $nWidth, $nHeight);
        $sPath_ = $sPath."Zoom/".$_FILES['sPicture']['name'];
        imagejpeg($nImg_, $sPath_);

        $sImg = imagecreatefromjpeg($sPath.'Original/'.$_FILES['sPicture']['name']);
        $nWidth = imagesx($sImg);
        $nHeight = imagesy($sImg);
        $nWidth_tmp = 120;
        $nHeight_tmp = 90;
        if($nWidth > $nWidth_tmp || $nHeight > $nHeight_tmp){
            if($nWidth < $nHeight){
                $nImageWidth = round(($nWidth * $nHeight_tmp) / $nHeight);
                $nImageHeight = $nHeight_tmp;
            }elseif($nWidth > $nHeight){
                $nImageWidth = $nWidth_tmp;
                $nImageHeight = round(($nHeight * $nWidth_tmp) / $nWidth);
            }else{
                $nImageWidth = $nWidth_tmp;
                $nImageHeight = $nHeight_tmp;
            }
        }else{
            $nImageWidth = $nWidth;
            $nImageHeight = $nHeight;
        }
        $nImg_ = imagecreatetruecolor($nImageWidth, $nImageHeight);
        imagecopyresampled($nImg_, $sImg, 0, 0, 0, 0, $nImageWidth, $nImageHeight, $nWidth, $nHeight);
        $sPath_ = $sPath."Thumb/".$_FILES['sPicture']['name'];
        imagejpeg($nImg_, $sPath_);

        $sInsert = "INSERT INTO tpicture(nPhotoID, lURL, sDescription) VALUES ('$nPhotoID', '$lURL1', '$sDescription1')";
        mysql_query($sInsert);
    }
}
mysqli_query($sConn,"INSERT INTO tblog(nUserID,nCategoryID,sTitle,sKeywords,sDescription,dDate,sPicture) VALUES('$nUserID_','$nCategoryID_','$sTitle_','$sKeywords_','$sDescription_','$dDate_','$sPicture_')");
?>
    
asked by anonymous 26.08.2015 / 03:12

1 answer

1

FIRST: The error in session_start(); , happens because the header has already been called before, put this function before the header.

SECOND: Every form uploading images should come with its enctype specified for: enctype="multipart/form-data" and put method="get" .

THIRD: error Undefined Variable , which may be happening because none of your conditions are setting a value for $_FILES['sPicture']['name'] . A suitable solution would be to treat the information better, something like this:

<?php

if(isset($_POST['submit'])){ //ponha um name para seu submit
   $name = $_FILES["sPicture"]["name"];  //ponha um name para seu type="file"    
   $tmp_name = $_FILES['sPicture']['tmp_name'];    

    if (isset ($name)) { //Caso não seja null
        if (!empty($name)) { //Caso não seja vazio


           //Aqui ficaria seu código para a o upload


        } //fim do empty
    } //fim do isset
} //fim do submit
?>
  Ready! do it all and return an answer here.

    
27.08.2015 / 01:50