Home » Php » javascript – Upload an excel in my view to display it in the controller

javascript – Upload an excel in my view to display it in the controller

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want…
Upload an excel file to my server in my view (only temporary because I will only read it, get its data and save it to a DB), then use fopen () on my controller, and open that file that the user uploaded in the view . (I am using CI)

Issue…
When I try to open the file in my controller it does not exist.

What I have…

view

<?php 
    print_r($_FILES);
    $archivo = fopen($_FILES["csv"]["tmp_name"], 'r');
?>

<?php echo form_open_multipart('',array('id' => 'envio', 'role'=>'form')); ?>
    <legend>Importar Datos</legend>
    <?php if (isset($alerta) && $alerta): ?>
        <div class="alert alert-danger">
            <?=$alerta ?>
        </div>

    <?php endif; ?>

    <div class="form-group">
        <label for="">Archivo CSV</label>
        <input type="file" class="form-control" id="csv" name="csv" placeholder="Archivo CSV">
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" value="1" name="has_header" checked>
            El archivo contiene cabeceras
        </label>
    </div>
    <input type="hidden" name="key" value="1">

    <button type="submit" class="btn btn-primary">Enviar</button>
</form>


<script type="text/javascript">
    function al_inicio()
    {
        console.log("on submit set");
        $( '#envio-t' )
        .submit( function( e ) {
            console.log('submitting');
            e.preventDefault();
            $.ajax( {
                // url: 'http://host.com/action/',
                type: 'POST',
                data: new FormData( this ),
                processData: false,
                contentType: false,
                dataType: 'script'
            } );
        } );
    }

</script>

Controller_api

    function dev_get(){
        print_r($_FILES);
        $archivo = fopen($_FILES["csv"]["tmp_name"], 'r');
    }

Images

This is in View

This is in Controller

How to&Answers: