file upload in php through ajax

Solutions on MaxInterview for file upload in php through ajax by the best coders in the world

showing results for - "file upload in php through ajax"
Sandro
15 Jan 2021
1**1. index.php**
2<body>
3    <span id="msg" style="color:red"></span><br/>
4    <input type="file" id="photo"><br/>
5  <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
6  <script type="text/javascript">
7    $(document).ready(function(){
8      $(document).on('change','#photo',function(){
9        var property = document.getElementById('photo').files[0];
10        var image_name = property.name;
11        var image_extension = image_name.split('.').pop().toLowerCase();
12
13        if(jQuery.inArray(image_extension,['gif','jpg','jpeg','']) == -1){
14          alert("Invalid image file");
15        }
16
17        var form_data = new FormData();
18        form_data.append("file",property);
19        $.ajax({
20          url:'upload.php',
21          method:'POST',
22          data:form_data,
23          contentType:false,
24          cache:false,
25          processData:false,
26          beforeSend:function(){
27            $('#msg').html('Loading......');
28          },
29          success:function(data){
30            console.log(data);
31            $('#msg').html(data);
32          }
33        });
34      });
35    });
36  </script>
37</body>
38
39**2.upload.php**
40<?php
41if($_FILES['file']['name'] != ''){
42    $test = explode('.', $_FILES['file']['name']);
43    $extension = end($test);    
44    $name = rand(100,999).'.'.$extension;
45
46    $location = 'uploads/'.$name;
47    move_uploaded_file($_FILES['file']['tmp_name'], $location);
48
49    echo '<img src="'.$location.'" height="100" width="100" />';
50}
51
Stefano
12 Jan 2017
1$('#upload').on('click', function() {
2    var file_data = $('#sortpicture').prop('files')[0];   
3    var form_data = new FormData();                  
4    form_data.append('file', file_data);
5    alert(form_data);                             
6    $.ajax({
7        url: 'upload.php', // point to server-side PHP script 
8        dataType: 'text',  // what to expect back from the PHP script, if anything
9        cache: false,
10        contentType: false,
11        processData: false,
12        data: form_data,                         
13        type: 'post',
14        success: function(php_script_response){
15            alert(php_script_response); // display response from the PHP script, if any
16        }
17     });
18});
19
Agustina
23 Sep 2018
1var formData = new FormData($("#YOUR_FORM_ID")[0]);
2$.ajax({
3    url: "upload.php",
4    type: "POST",
5    data : formData,
6    processData: false,
7    contentType: false,
8    beforeSend: function() {
9
10    },
11    success: function(data){
12
13
14
15
16    },
17    error: function(xhr, ajaxOptions, thrownError) {
18       console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
19    }
20});
21
Aurore
14 Feb 2019
1async function saveFile() 
2{
3    let formData = new FormData();           
4    formData.append("file", sortpicture.files[0]);
5    await fetch('/uploads', {method: "POST", body: formData});    
6    alert('works');
7}
Gisselle
21 Jan 2018
1<?php
2
3    if ( 0 < $_FILES['file']['error'] ) {
4        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
5    }
6    else {
7        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
8    }
9
10?>
11
Latoya
26 Nov 2016
1<?
2$data = array();
3    //check with your logic
4    if (isset($_FILES)) {
5        $error = false;
6        $files = array();
7
8        $uploaddir = $target_dir;
9        foreach ($_FILES as $file) {
10            if (move_uploaded_file($file['tmp_name'], $uploaddir . basename( $file['name']))) {
11                $files[] = $uploaddir . $file['name'];
12            } else {
13                $error = true;
14            }
15        }
16        $data = ($error) ? array('error' => 'There was an error uploading your files') : array('files' => $files);
17    } else {
18        $data = array('success' => 'NO FILES ARE SENT','formData' => $_REQUEST);
19    }
20
21    echo json_encode($data);
22?>
23
Marcy
21 Feb 2017
1move_uploaded_file(
2
3    // this is where the file is temporarily stored on the server when uploaded
4    // do not change this
5    $_FILES['file']['tmp_name'],
6
7    // this is where you want to put the file and what you want to name it
8    // in this case we are putting in a directory called "uploads"
9    // and giving it the original filename
10    'uploads/' . $_FILES['file']['name']
11);
12
queries leading to this page
upload file use ajaxajax image file upload proccessupload file with ajax phpajax upload image phpjquery ajax php post temporary location of files to fieldphp upload php file ajaxupload photo ajax php mysqlhow to upload file in ajax phpimage upload in ajax phpajax upload file with phpajaxfile phpupload file in php with ajaxphp ajax form with file uploadupload image to database using jquery 2fajaxajax post file upload phpajax query to use fileupload and show uploaded file in image element in laravelphp javascript upload file ajaxupload image using jquery ajax phpfile upload with ajax in phpajax send a file to php serverajax post upload filehow to upload file in php using ajaxajax upload fileajax file upload jquery example phpupload file in php with ajax and javascriptphp file upload with jquery ajaximage upload by ajax in phphow to upload file using ajax in phpimage upload using ajax in phpfile upload html using ajaxajaxfileupload phpphp file upload example ajaxfile upload in php through ajaxhow to send file with ajax to phphhow do i upload a file in ajax without using a form in php 3fupload data in db using ajax phpphp upload file formupload file on ajaxsubmit form with file upload using ajax phphow to upload file in php using ajax in project upload folderupload file php ajaxusing ajax 2f jquery to post image and text data to databaseupload image ajax jquery php mysqlhow toget file as well as text in ajax jquery pj mysqlfile upload with ajax in core phpajax php file uploadfile upload in ajaxupload image using ajax in phpupload with ajax phpphp file upload ajax examplephp form upload file examplesubmit form with file field upload using ajax phpimage upload with ajax in phpsimple ajax file upload jquery phpjquery ajax form php file uploadsrc ajax jquery get data video php fileupload image ajax phphow to create upload file in ajax method in jquery using php apiphp ajax upload file exampleupload image ajax jquery 2c php mysqlfile upload to database with ajax in phpupload com ajax phphow to upload file in form php using ajaximage upload php ajaxhow to upload file using jquery ajax phpimage upload using ajax phphtml form file upload ajax phphow can database image in html using ajax phpupload file with ajax jquery phpajax file upload table phphow to upload file with ajax in phpajax post upload file phpdurring file uploading value shoud go to db jqueryhow does php ajax temp upload image workupdate file in ajax phphow to upload a file in php using ajaxsubmit form data 2b file using ajax in phpphp server ajax send filephp file upload with ajaxphp ajax form submit with file uploaduplaod and send file php jqueryupload image with jquery and phphow to process post image data sent through javascript at phpjqeury php upload file and sent input textfile upload in php ajaxhow ajax file upload php worksphp ajax file uploadupload file using javascript ajax phpfile upload via ajax pgpsave and upload data and image in ajax formjavascript ajax php file uploadimage upload in php using javascripthandle file upload php ajaxupload file in ajaxcomplete form data submit jquery php imagephp ajax file upload libraryfile upload jquery ajax phpfile upload in ajax with form dataphp upload file ajaxajax file upload phpphp image upload using ajaxupload jquery ajax phpfile upload by ajax in phpsend file to php via jqueryuplood file with ajax phpajax post to file uploadphp ajax send filehow to upload file using jquery ajax in phpphp upload file ajaz requestupload file through ajax phpreceive file from ajax phpfile upload with ajax phpjquery php upload and returnajax image upload in phpuploading video via ajax php not received datause ajax for file upload phpajax upload file to web apihow to upload a form containing image and data to the database using jquery ajaxhow to upload file with jquery ajax phpinsert image in php ajaxupload file using ajax phpjquery ajax php file uploadphp ajax form upload file examplesave file with ajax jquery phpfile upload using ajax phpsend file from jquery to phpajax file upload in phpformdata php ajax file uploadupload file using ajax server daqtabaseimage upload in php using ajaxajax file upload using php example codephp jquery upload filesphp jquery ajax file uploadsend files via ajax in phpfile upload with php and jqueryform upload file php mysqlfile upload and share in php using ajaxupload file using ajaxsend upload file in ajaxphp upload file to database with ajaxajax php script remote uploaderupload file with ajaxhow to upload file by ajax in phpjquery ajax post upload fileajax image uploadphp ajax uploadimage upload in php ajaxhow to get ajax image file in phphow to upload file and send text using ajax phpjquery ajax upload file phpupload file and insert uploaded url ajaxphp upload file form ajaxphp upload file ajax jqueryform having image submission using ajax in phpjquery ajax post file upload exampleupload file in sicurezza php htmljquery ajax php save filehow to upload images to a folder using ajax php and jqueryupload file on php ajaxhow to upload image in ajax phpupload file and data using jquery ajax and rest api phpupload image in ajax php scriptupload file using ajax in php examplephp ajax post file uploadphp upload file jquery ajaxphp jquery upload filephp file on upload ajaxuploading file via ajax rest phploading object tag using ajax phpajax php upload document file how to upload file and receive using ajax in javascript 2cphpphp file upload ajaxjquery send file via ajax to phpfile upload with jquery and ajaxupload files using jquery and phpjquery file upload intergrate phpload file via ajax phpupload file ajax phpuploading picture to a website and saving the files into the website js to phpupload file via ajaxfile upload through ajaxphp ajax file upload in loopupload files in ajaxget file from ajax in to phpsample file upload using ajax and phpajax php upload fileimage upload ajax processing phpfile upload in ajax phpfile upload with ajaxupload image to db with ajax jquerypass file from ajax phpfile upload using ajaxajax file upload requestajax create post with fileajax image upload with other data phpupload image using ajax phpajax php uploadphp upload file parameters with ajaxajax php upload document how to upload file in ajaxphp choose file from server with ajaxajax upload file php mysqlfile upload ajax phpmpphp6f30 tmp error insert photo mysql ajax phpupload file by ajaxchoose a file from directory in php with ajax requestfile upload using ajax postis uploading a file using ajax possible phpjqeury submit file to php serverajax upload file to phphow to send a file to php by ajaxphp upload with ajaxajax fprm submission with file upload in phpphp jquery ajax validate uploaded tutorialpost file content using ajaxmove uploaded file in ajaxhow to update upload file in php using ajaxajax form file upload phpipload image php ajaxform file upload ajax in phpajax upload form with fileajax upload file phpajax file upload using phpphp upload file to ajax using onchangefile uploader with jquery and ajaxsend file ajax phpupload file ajax jquery phpusing ajax file upload in phpscript to upload image in php ajaxupload file in php using ajaxupload file jquery ajax phphow to upload value with ajax in phpsimple php image upload script ajaximage validation in php using jqueryphp upload ajaxupload a file without jquery using ajax phpfile upload ajax and phpfile upload ajax jquery phpjquery php file insertupload a file using ajax and phpphp ajax upload fileupload image in php ajax jqueryjquery php upload fileajax image upload in php ajaxjquery send upload file to phpupload file via ajax phpsend and receive file input in php via jquery ajaxupload docs in php through ajaxajax file upload with form data using phpfile upload php ajaxajax add file upload php to formhow to upload image using ajax in phpupload image using php and ajaxajax form submit with file upload in phpimage uploading using ajax in phpfile upload in php jqueryupload with ajax phpupload image with ajax phphow send file in form with ajax to phpfile upload in php through ajax