4
I created the following form below on a client’s website, so that you can change a user’s photo:
<form id="formulario" method="post" enctype="multipart/form-data">
<div class="blocoCampo">
<span> Escolher nova foto (dimensões: 50px x 50px; formato: JPG) </span>
<input type="file" id="imgupload" name="imgupload">
</div>
<div class="confirmarTrocaSenha">
<input type="button" value="" id="btn_troca_foto">
</div>
</form>
Then, I created in the function below (javascript) for him to check the extension and the measurements in Pixels of the file. It should always be 50x50 and JPG:
var _URL = window.URL || window.webkitURL;
$("#imgupload").change(function (e) { // Aqui verifica as dimensões e a extensão do arquivo
var foto = $('#imgupload').val();
var extensao = foto.substr(foto.length - 3);
var file, img;
if ((file = this.files[0])) {
img = new Image();
img.onload = function () {
if(this.width !== 50){
alert('Dimensões não permitidas!');
$("#imgupload").val('');
}
else if(this. height !== 50){
alert('Dimensões não permitidas!');
$("#imgupload").val('');
}
else if(extensao != 'jpg'){
alert('Extensão não permitida!');
$("#imgupload").val('');
}
};
img.src = _URL.createObjectURL(file);
}
});
And below is the function that should host the photo, after it is loaded:
$('#btn_troca_foto').click(function(){
var imgupload = $('#imgupload').val();
if(imgupload == ''){
alert("Nenhuma foto selecionada!");
}
else{
$.ajax({
url: "trocarfoto",
type: "POST",
data: { imgupload: imgupload },
success:function(a){
alert(a);
$('.contentLightbox, .bgLightbox').fadeOut();
}
});
}
});
And the function link trochanter is in PHP below:
public static function trocaFoto() {
@mkdir('upload/wt_usuario', 0777);
$target = "upload/wt_usuario/" . $_SESSION['usuario']['id'] . ".jpg";
move_uploaded_file($_FILES['imgupload']['tmp_name'], $target);
echo "Foto de perfil alterada";
}
Everything working except the function part move_uploaded_file, that should take the file that is in the input imgupload and transfer to the directory on the server of my site.
What could be wrong or missing? I know that searching the internet has several ways to do it, but I believe that only some details are missing in my code.
I appreciate anyone who can help.
I got the answer I wanted on another forum and it worked. Thanks anyway. ;)
– Gustavo Hoppe Levin