October 14, 2016

Uploading image to the server via jQuery Ajax and php

I was solving a problem with uploading image to the server via jQuery Ajax. Finally I got a solution. HTML





CART


Javascript:




PHP script (just the simple solution, add some validation )

    //Custom data - Sent Via AJAX post method
    $file = $_POST['file']; //This is image sent via AJAX
    $fileName = $_POST['fileName'];    
    $brandingDesc = $_POST['brandingDesc'];
           
    $serverFile = time().$fileName;
 
    $uploads = wp_upload_dir();
    $upload_path = $uploads['basedir'];
 
    $serverFileName = $upload_path . "/" . $serverFile;
 
    //Get the base-64 string from data
    $filteredData=substr($_POST['file'], strpos($_POST['file'], ",")+1);
 
    //Decode the string
    $unencodedData=base64_decode($filteredData);
 
    //Save the image
    file_put_contents($serverFileName, $unencodedData);

No comments:

Post a Comment