When I upload a file, it first does the progress bar with JS, and then it moves the file that has been uploaded to the folder. The problem is that it ignores echo 'começo'
and 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 that case it also ignores if a file is uploaded, and I'd like to be able to "control" the moment the file is uploaded, not when pressing the submit form.
Code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script><scriptsrc="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';
}
?>