Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ajax
- $.ajax({
- url:'/admin/upload/image',
- method: 'POST',
- data: form,
- contentType: false,
- processData: false,
- //Pegando o recurso de progresso nativo do JQUERY para o javascript
- xhr: function(){
- const xhr = $.ajaxSettings.xhr();
- xhr.upload.addEventListener('progress', function (e){
- //calculando a porcentagem
- let progress = e.loaded / e.total * 100;
- //Passando a porcentagem carregada
- attachment.setUploadProgress(progress);
- });
- return xhr;
- }
- }).done(function(resp){
- //console.log('resp');
- console.log(resp);
- //PHP
- elseif(resolve('/admin/upload/image')){
- $file = !empty($_FILES['file'])? $_FILES['file'] : NULL;
- if(!$file){
- (422);
- //echo json_encode(['status' => 'Nenhum arquivo enviado']);
- echo 'Nenhum arquivo enviado';
- exit;
- }
- $allowedType =[
- 'image/gif',
- 'image/jpg',
- 'image/jpeg',
- 'image/png',
- ];
- //Se não estiver nos tipos permitidos
- if(!in_array($file['type'], $allowedType)){
- http_response_code(422);
- echo 'Arquivo não permitido, utilize arquivos: gif, jpg, jpeg ou png';
- exit;
- }
- //Criando um nome randomico para o arquivo
- $name = uniqid(rand(), true) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
- //Movendo a imagem para o diretorio no servidor
- move_uploaded_file($file['tmp_name'], __DIR__.'/../public/upload/'. $name);
- //print_r($file);
- echo '/upload/'. $name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement