Created
May 21, 2015 17:00
-
-
Save mayroncachina/69d2fedc5591dbfc2e00 to your computer and use it in GitHub Desktop.
Upload with base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function saveImage($base64img){ | |
if($base64img == ""){ | |
return "0"; | |
}else{ | |
define('UPLOAD_DIR', 'upload/'); | |
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img); | |
$data = base64_decode($base64img); | |
$file = UPLOAD_DIR . makeRandomString(8).'.jpg'; | |
if(file_put_contents($file, $data)){ | |
return 'http://'.$_SERVER['SERVER_NAME'].'/'.$file; | |
}else{ | |
return "0"; | |
} | |
} | |
} | |
function makeRandomString($max=6) { | |
$i = 0; //Reset the counter. | |
$possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
$keys_length = strlen($possible_keys); | |
$str = ""; //Let's declare the string, to add later. | |
while($i<$max) { | |
$rand = mt_rand(1,$keys_length-1); | |
$str.= $possible_keys[$rand]; | |
$i++; | |
} | |
return $str; | |
} | |
//echo saveImage($_POST['file']); | |
print_r($_FILES); | |
$new_image_name = makeRandomString(); | |
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_image_name.".jpg"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment