Upload file not following code

Asked

Viewed 26 times

1

When I upload a file, first it makes the progress bar with JS, and then it moves the file that was uploaded to the folder. The problem is he ignores the echo 'começo' and the echo 'final', and I’d like to do a test there, something like:

$name = $_POST['nameSendTorrent']; 
if(empty($name)){echo 'Precisa colocar um nome'; return;}

And in this case it also ignores if a file is uploaded, and I would like to be able to "control" the moment when the file is uploaded, not when you press the form Ubmit.

Code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script type="text/javascript">   
$(function() 
{
    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');

    $('form').ajaxForm(
    {
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) 
        {
            document.getElementById("progressBarCurrent").style.display = 'block';
            var total_perc = total | 0;
            var current_perc = position | 0;
            document.getElementById("progbar").innerHTML = Math.floor((current_perc / (total_perc / 100)) * 100) / 100 + '%';
            document.getElementById("progbar").style.width = current_perc / (total_perc / 100) + '%';
        },
        complete: function(xhr) {
            status.html(xhr.responseText);
        }
    });
}); 
</script>

<form enctype="multipart/form-data" method="post" class="formSendTorrent"  id="formSendTorrent" name="formSendTorrent">
        <div class="defaultStyleSend fontDefault" id="defaultStyleSend">
            <a>Nome:</a> 
            <input type="text" class="nameSendTorrent" name="nameSendTorrent">
        </div> 
        <div class="defaultStyleSend fontDefault">
            <input type="file" multiple name="inputfileSendTorrent[]" id="inputfileSendTorrent"> 
        </div>
        <div id="progressBarCurrent">
            <div id="progbar"></div>
        </div>

        <input type="submit" name="submitSendTorrent" class="submitSendTorrent" value="Enviar">
    </form>

<?php
require DIR_FUNCS.'funcSQL.php';
require_once DIR_FUNCS.'Torrent.php';

if(isset($_POST['submitSendTorrent']))
{   
    echo 'começo';

    $i = 0;

    $uploaddir = DIR_ARQUIVOS.$chave.'/';

    foreach ($_FILES["inputfileSendTorrent"]["error"] as $key => $error) 
    {
        $arqName = $_FILES['inputfileSendTorrent']['name'][$i];
        $arqTemp = $_FILES['inputfileSendTorrent']['tmp_name'][$i];

        if(!@move_uploaded_file($arqTemp, $uploaddir.$arqName))
        {
            $error = error_get_last();
            echo $error['message'];
        }

        echo '1';

        $i++;
    }

    echo 'final';
} 
?>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.