AJAX Post faylini PHP faylga yuklash nazorati

AJAX dan foydalanib men PHP fayliga faylni yuklashni boshqarish qiymatini joylashtirmoqdaman.

Fayl yuklanishini nazorat qilish:

<input class="form-control" type="file" id="policy_image" name="policy_image" accept="image/*">

Quyidagi AJAX qo'ng'iroqlari:

var file_data = $('#policy_image').prop('files')[0];   
var policy_image = new FormData();                  
policy_image.append('file', file_data);

$.ajax({
    type: 'post',
    url: 'addpolicy.php', 
    data: {
        'policy': $('#policy').val(),
        'policy_image': policy_image
    },                
    enctype: 'multipart/form-data',
    processData: false,
    contentType: false,             
    success: function(data) {
        alert('Success');
    }
});

PHP faylimda quyida ko'rsatilgan qiymatni olaman

PHP fayli:

$name = $_POST['policy'];

$imgFile = $_FILES['file']['name'];
$tmp_dir = $_FILES['file']['tmp_name'];
$imgSize = $_FILES['file']['size'];


  $upload_dir = 'assets/img/policy/';//upload directory

  $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));//get image extension

 //valid image extensions
  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');//valid extensions

 //rename uploading image
  $userpic = rand(1000,1000000).".".$imgExt;

 //allow valid image file formats
  if(in_array($imgExt, $valid_extensions)){     
   //Check file size '5MB'
    if($imgSize < 5000000)        {
      move_uploaded_file($tmp_dir,$upload_dir.$userpic);
    }
    else{
      $errMSG = "Sorry, your file is too large.";
    }
  }
  else{
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";    
  }


// if no error occured, continue ....
if(!isset($errMSG))
{
  $stmt = $DB_con->prepare('INSERT INTO policy(name,image) VALUES(:name, :image)');
  $stmt->bindParam(':name',$name);
  $stmt->bindParam(':image',$userpic);

  if($stmt->execute())
  {
    $successMSG = "new record succesfully inserted ...";
  }
  else
  {
    $errMSG = "error while inserting....";
  }
}

Endi Undefined indeks: policy va Undefined indeks: policy_image kabi xatolarga duch kelaman

3
FormData'da policy_image emas, balki fayl
qo'shib qo'ydi muallif Rory McCrossan, manba
FormData'da policy_image emas, balki fayl
qo'shib qo'ydi muallif Rory McCrossan, manba
@RoryMcCrossan shuning uchun $ _ fayllar ["fayl"] ["ism"] deb nomlanishi kerakmi? bunga o'xshashmi? hatto bu Undefined indeks: file kabi ko'rsatiladi
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba

6 javoblar

Buni ko'ring:

var file_data = $('#policy_image').prop('files')[0];
var form_data = new FormData();
form_data.append('policy_image', file_data);

$.ajax({
    url         : 'addpolicy.php',
    cache       : false,
    contentType : false,
    processData : false,
    data        : form_data,                         
    type        : 'post',
    success     : function(output){
        alert(output);
    }
});

PHP:

if ( $_FILES['policy_image']['error'] > 0 ){
        echo 'Error: ' . $_FILES['policy_image']['error'] . '
'; } else { if(move_uploaded_file($_FILES['policy_image']['tmp_name'], 'uploads/' . $_FILES['policy_image']['name'])) { echo "File Uploaded Successfully"; } }
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
AJAX : {'policy': $ ('# policy') parametrlari orqali yana bitta ma'lumot yozyapman. Val (), 'policy_image': policy_image},
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba

Buni ko'ring:

var file_data = $('#policy_image').prop('files')[0];
var form_data = new FormData();
form_data.append('policy_image', file_data);

$.ajax({
    url         : 'addpolicy.php',
    cache       : false,
    contentType : false,
    processData : false,
    data        : form_data,                         
    type        : 'post',
    success     : function(output){
        alert(output);
    }
});

PHP:

if ( $_FILES['policy_image']['error'] > 0 ){
        echo 'Error: ' . $_FILES['policy_image']['error'] . '
'; } else { if(move_uploaded_file($_FILES['policy_image']['tmp_name'], 'uploads/' . $_FILES['policy_image']['name'])) { echo "File Uploaded Successfully"; } }
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
AJAX : {'policy': $ ('# policy') parametrlari orqali yana bitta ma'lumot yozyapman. Val (), 'policy_image': policy_image},
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba

Buni ko'ring:

var file_data = $('#policy_image').prop('files')[0];
var form_data = new FormData();
form_data.append('policy_image', file_data);

$.ajax({
    url         : 'addpolicy.php',
    cache       : false,
    contentType : false,
    processData : false,
    data        : form_data,                         
    type        : 'post',
    success     : function(output){
        alert(output);
    }
});

PHP:

if ( $_FILES['policy_image']['error'] > 0 ){
        echo 'Error: ' . $_FILES['policy_image']['error'] . '
'; } else { if(move_uploaded_file($_FILES['policy_image']['tmp_name'], 'uploads/' . $_FILES['policy_image']['name'])) { echo "File Uploaded Successfully"; } }
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
AJAX : {'policy': $ ('# policy') parametrlari orqali yana bitta ma'lumot yozyapman. Val (), 'policy_image': policy_image},
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
    upload.php
        $('#upload').on('click', function() {
            var file_data = $('#sortpicture').prop('files')[0];   
            var form_data = new FormData();                  
            form_data.append('file', file_data);
            alert(form_data);                             
            $.ajax({
                        url: 'addpolicy.php',//point to server-side PHP script 
                        dataType: 'text', //what to expect back from the PHP script, if anything
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,                         
                        type: 'post',
                        success: function(response){
                            alert(response);
                        }
             });
        });
    addpolicy.php
    <?php

        if ( 0 < $_FILES['file']['error'] ) {
            echo 'Error: ' . $_FILES['file']['error'] . '
'; } else { move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']); echo "true"; } ?>
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
Hali ham Undefined indeks: policy va Undefined indeks: file ni olaman. O'ylaymanki, men boshqa xatolik qilyapman.
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
    upload.php
        $('#upload').on('click', function() {
            var file_data = $('#sortpicture').prop('files')[0];   
            var form_data = new FormData();                  
            form_data.append('file', file_data);
            alert(form_data);                             
            $.ajax({
                        url: 'addpolicy.php',//point to server-side PHP script 
                        dataType: 'text', //what to expect back from the PHP script, if anything
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,                         
                        type: 'post',
                        success: function(response){
                            alert(response);
                        }
             });
        });
    addpolicy.php
    <?php

        if ( 0 < $_FILES['file']['error'] ) {
            echo 'Error: ' . $_FILES['file']['error'] . '
'; } else { move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']); echo "true"; } ?>
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
Hali ham Undefined indeks: policy va Undefined indeks: file ni olaman. O'ylaymanki, men boshqa xatolik qilyapman.
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
    upload.php
        $('#upload').on('click', function() {
            var file_data = $('#sortpicture').prop('files')[0];   
            var form_data = new FormData();                  
            form_data.append('file', file_data);
            alert(form_data);                             
            $.ajax({
                        url: 'addpolicy.php',//point to server-side PHP script 
                        dataType: 'text', //what to expect back from the PHP script, if anything
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,                         
                        type: 'post',
                        success: function(response){
                            alert(response);
                        }
             });
        });
    addpolicy.php
    <?php

        if ( 0 < $_FILES['file']['error'] ) {
            echo 'Error: ' . $_FILES['file']['error'] . '
'; } else { move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']); echo "true"; } ?>
0
qo'shib qo'ydi
pls mening tahririmni ko'rishga. Buning ortidan narsalarimni qilaman. Sizning kodingiz bilan qanday qilib integratsiya qilishni o'ylayapman?
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
Hali ham Undefined indeks: policy va Undefined indeks: file ni olaman. O'ylaymanki, men boshqa xatolik qilyapman.
qo'shib qo'ydi muallif Maria Jeysingh Anbu, manba
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb

PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin