file upload in php through ajax

**1. index.php**
<body>
    <span id="msg" style="color:red"></span><br/>
    <input type="file" id="photo"><br/>
  <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $(document).on('change','#photo',function(){
        var property = document.getElementById('photo').files[0];
        var image_name = property.name;
        var image_extension = image_name.split('.').pop().toLowerCase();

        if(jQuery.inArray(image_extension,['gif','jpg','jpeg','']) == -1){
          alert("Invalid image file");
        }

        var form_data = new FormData();
        form_data.append("file",property);
        $.ajax({
          url:'upload.php',
          method:'POST',
          data:form_data,
          contentType:false,
          cache:false,
          processData:false,
          beforeSend:function(){
            $('#msg').html('Loading......');
          },
          success:function(data){
            console.log(data);
            $('#msg').html(data);
          }
        });
      });
    });
  </script>
</body>

**2.upload.php**
<?php
if($_FILES['file']['name'] != ''){
    $test = explode('.', $_FILES['file']['name']);
    $extension = end($test);    
    $name = rand(100,999).'.'.$extension;

    $location = 'uploads/'.$name;
    move_uploaded_file($_FILES['file']['tmp_name'], $location);

    echo '<img src="'.$location.'" height="100" width="100" />';
}

5
4
MartinSGill 100 points

                                    async function saveFile() 
{
    let formData = new FormData();           
    formData.append(&quot;file&quot;, sortpicture.files[0]);
    await fetch('/uploads', {method: &quot;POST&quot;, body: formData});    
    alert('works');
}

5 (4 Votes)
0
0
0
N M 110 points

                                    var formData = new FormData($(&quot;#YOUR_FORM_ID&quot;)[0]);
$.ajax({
    url: &quot;upload.php&quot;,
    type: &quot;POST&quot;,
    data : formData,
    processData: false,
    contentType: false,
    beforeSend: function() {

    },
    success: function(data){




    },
    error: function(xhr, ajaxOptions, thrownError) {
       console.log(thrownError + &quot;\r\n&quot; + xhr.statusText + &quot;\r\n&quot; + xhr.responseText);
    }
});

0
0
4.4
5
Runcy Oommen 100 points

                                    $('#upload').on('click', function() {
    var file_data = $('#sortpicture').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    alert(form_data);                             
    $.ajax({
        url: 'upload.php', // point to server-side PHP script 
        dataType: 'text',  // what to expect back from the PHP script, if anything
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(php_script_response){
            alert(php_script_response); // display response from the PHP script, if any
        }
     });
});

4.4 (5 Votes)
0
0
2
0112 110 points

                                    &lt;?php

    if ( 0 &lt; $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '&lt;br&gt;';
    }
    else {
        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    }

?&gt;

0
0
4
2
Ukminam 110 points

                                    move_uploaded_file(

    // this is where the file is temporarily stored on the server when uploaded
    // do not change this
    $_FILES['file']['tmp_name'],

    // this is where you want to put the file and what you want to name it
    // in this case we are putting in a directory called &quot;uploads&quot;
    // and giving it the original filename
    'uploads/' . $_FILES['file']['name']
);

4 (2 Votes)
0
3.67
3

                                    &lt;?
$data = array();
    //check with your logic
    if (isset($_FILES)) {
        $error = false;
        $files = array();

        $uploaddir = $target_dir;
        foreach ($_FILES as $file) {
            if (move_uploaded_file($file['tmp_name'], $uploaddir . basename( $file['name']))) {
                $files[] = $uploaddir . $file['name'];
            } else {
                $error = true;
            }
        }
        $data = ($error) ? array('error' =&gt; 'There was an error uploading your files') : array('files' =&gt; $files);
    } else {
        $data = array('success' =&gt; 'NO FILES ARE SENT','formData' =&gt; $_REQUEST);
    }

    echo json_encode($data);
?&gt;

3.67 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to upload file in ajax php upload file in php with ajax image upload in php ajax how to create upload file in ajax method in jquery using php api file upload html using ajax how to upload image using ajax in php how to upload file using jquery ajax in php file upload to database with ajax in php file upload with ajax in php form file upload ajax in php php upload ajax upload jquery ajax php use ajax for file upload php PHP upload file to Ajax using onchange AJAX UPLOAD FILE TO PHP upload image using ajax in php php ajax form with file upload image upload using ajax php upload image using ajax php php upload file form ajax upload a file using ajax and php php file upload ajax example ajax image upload with other data php how to upload image in ajax php php upload file ajax file upload ajax php ajax file upload with form data using php file upload with ajax in core php html form file upload ajax php jquery ajax upload file php ajax file upload in php file upload in ajax with form data ajax file upload table php how to upload file with ajax in php file upload php ajax ajax add file upload php to form ajax php file upload javascript ajax php file upload upload file on ajax ajax image upload in php ajax upload file use ajax upload file in ajax image upload using ajax in php upload file jquery ajax php upload file using ajax in php example submit form with file field upload using ajax php submit form with file upload using ajax php upload file with ajax jquery php ajax upload file php mysql send upload file in ajax image upload with ajax in php upload file using javascript ajax php ajax file upload using php example code ajax file upload using php php ajax upload file php ajax post file upload upload file using ajax php uploading file via ajax rest php php ajax upload php javascript upload file ajax upload file and data using jquery ajax and rest api php how to upload file with jquery ajax php php file upload with ajax upload image using php and ajax how to upload file using jquery ajax php image upload in php using ajax how to upload file in ajax file upload with php and jquery uplood file with ajax php upload image with ajax php php image upload using ajax upload file via ajax php upload image in ajax php script upload file by ajax ajax upload file php ajax post file upload php how to upload file using ajax in php ajax upload form with file how to upload file and send text using ajax php how ajax file upload php works simple ajax file upload jquery php ajax upload file php ajax form submit with file upload script to upload image in php ajax php upload file to database with ajax ajax file upload php file upload using ajax post image upload by ajax in php file upload by ajax in php file upload with ajax how to upload file in php using ajax in project upload folder how to upload file in php using ajax post file content using ajax Upload file using AJAX jquery ajax form php file upload upload file with ajax php how to upload file and receive using ajax in javascript,php ajax form file upload php upload file in php using ajax file upload in php through ajax upload file ajax jquery php upload file php ajax ajax form submit with file upload in php upload a file without jquery using ajax php upload file with ajax php upload file jquery ajax ajax post upload file php php file upload ajax php ajax file upload library how to upload file by ajax in php upload file ajax php php upload with ajax file upload ajax and php php ajax file upload in loop file upload in ajax sample file upload using ajax and php php ajax file upload image upload in ajax php is uploading a file using ajax possible php ajax upload file to web api send file to php via jquery upload with ajax. php Upload File using Ajax server daqtabase jquery php file insert upload image to db with ajax jquery ajax upload image php ipload image php ajax form having image submission using ajax in php jquery ajax php post temporary location of files to field how to upload file in form php using ajax ajax image upload in php how to upload value with ajax in php How to upload images to a folder using ajax php and jquery image upload in php using javascript uploading video via ajax php not received data upload image using jquery ajax php ajax file upload request image uploading using ajax in php using ajax / jquery to post image and text data to database image upload ajax processing php src ajax jquery get data video php file how to get ajax image file in php php upload file form upload photo ajax php mysql load file via ajax php upload image ajax jquery php mysql uploading picture to a website and saving the files into the website js to php jqeury submit file to php server ajax send a file to php server ajax image file upload proccess jquery send upload file to php jquery ajax php save file ajax fprm submission with file upload in php php jquery ajax validate uploaded tutorial php jquery upload file jqeury php upload file and sent input text php ajax upload file example durring file uploading value shoud go to db jquery php ajax form upload file example php form upload file example uplaod and send file php jquery php server ajax send file php ajax send file ajaxfile.php send file from jquery to php upload image to database using jquery/ajax how send file in form with ajax to php how to send file with ajax to phph php upload file ajax jquery form upload file php mysql upload file in sicurezza php html image upload php ajax php upload file ajaz request php jquery ajax file upload upload image in php ajax jquery php file upload example ajax get file from ajax in to php complete form data submit jquery php image send file ajax php file upload using ajax move_uploaded_file in ajax ajaxfileupload php pass file from ajax php receive file from ajax php ajax create post with file jquery php upload file file upload with jquery and ajax file uploader with jquery and ajax upload docs in php through ajax upload image with jquery and php ajax post upload file How to upload a form containing image and data to the database using jquery ajax ajax php upload document file ajax php upload document ajax php upload file how to send a file to php by ajax choose a file from directory in php with ajax request file upload via ajax pgp save file with ajax jquery php ajax image upload jquery ajax php file upload send and receive file input in php via jquery ajax file upload in php jquery upload files using jquery and php ajax query to use fileupload and show uploaded file in image element in laravel php choose file from server with ajax php jquery upload files file upload jquery ajax php using ajax file upload in php submit form data + file using ajax in php save and upload data and image in ajax form mpphp6F30.tmp error insert photo mysql ajax php upload image ajax jquery, php mysql simple php image upload script ajax loading object tag using ajax php php file upload with jquery ajax send files via ajax in php file upload ajax jquery php how does php ajax temp upload image work ajax upload file with php insert image in php ajax formdata php ajax file upload how can database image in html using ajax php jquery php upload and return how toget file as well as text in ajax jquery pj mysql upload file on php ajax php file on upload ajax jquery send file via ajax to php jquery file upload intergrate php image validation in php using jquery file upload in php ajax how to process post image data sent through javascript at php file upload with ajax php file upload in ajax php update file in ajax php
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source