Create such an input element,
<form id="fileupf" action="fileup.php" method="post" enctype="multipart/form-data"> <input type="file" id="fileup" name="file" accept="image/*" > </form>
Then in ajax,
$('#fileupf').ajaxForm({ complete: function(xhr) { alert("Upload complete"); } });
In php,
<?php $target_path = "uploaded_images/"; $file_name = $_FILES["file"]["name"]; $random_digit=rand(0000,9999); $new_file_name=$random_digit.$file_name; $target_path = $target_path . basename( $new_file_name); ?>
If you save a canvas drawing,
Javascript
var canvasData = canvas.toDataURL("image/png"); var ajax = new XMLHttpRequest(); ajax.open("POST",'save.php',false); ajax.setRequestHeader('Content-Type', 'application/upload'); ajax.send(canvasData);
Php
<?php if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) { // Get the data $imageData=$GLOBALS['HTTP_RAW_POST_DATA']; $filteredData=substr($imageData, strpos($imageData, ",")+1); $unencodedData=base64_decode($filteredData); $random_digit=md5(uniqid(mt_rand(), true)); $fp = fopen( 'yourfolder/new'.$random_digit.'.png', 'wb' ); fwrite( $fp, $unencodedData); fclose( $fp ); } ?>
source share