ajax download of images



  • Help me find a mistake in the processors of the incoming servers.

    Server doesn't bring anything back when it's worth checking. isset( $_FILES['files'] ), but if put isset( $_FILES )he's starting to bring something back.

    Form:

    <input title="Выберите файл для загрузки" id="upload_media" type="file" name="files" accept=".txt,image/*">
    

    Processor:

    $('input[type=file]').change(function(){
        Upload.files = this.files[0];
        var upload = Upload.upload();
    });
    

    var Upload = {
    files: 0,
    upload: function() {

    var data = new FormData();
    $.each( Upload.files, function( key, value ){
        data.append( key, value );
    });
    
    $.ajax({
        url: "upload.php",
        type: "POST",
        data: data,
        contentType: false,
        cache: false,
        processData:false,
        success: function( reply ){
            alert( "test. " + reply );
        },
        error: function( reply ){
            console.log('ОШИБКИ AJAX запроса: ' + reply.responseText );
        }
    });
    

    },
    }

    Server:

    if ( isset( $_FILES['files'] ) ) {
    $error = false;
    $status = false;
    $files = [];

    $uploaddir = './uploads/';
    $files = $_FILES;
    $uploadfile = $uploaddir . basename($files['name']);
    
    if (move_uploaded_file( $files['tmp_name'], $uploadfile) ) {
        echo "Файл загружен";
    } else {
        echo "Ошибка";
    }
    
    echo json_encode( $files['name'] );
    

    }



  • isset( $_FILES['files']) It doesn't work as far as I know.

    See if you can condition this:

    if ($_FILES['files']['error'] === UPLOAD_ERR_OK and $_FILES['files']['size'] != 0) {
        // загрузка прошла успешно
    }else{
        // загрузка не удалась
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2