Home >Backend Development >PHP Tutorial >PHP realizes pasting screenshots and completing the upload function_PHP tutorial
Today I found that you can paste and upload pictures in the comments of segmentfault, so I studied how to achieve it!
The principle is very simple. In fact, it is to monitor the paste event, and then detect whether there are pictures in the pasted things. If so, it will directly trigger the ajax upload
The code can be run directly. If you are interested, you can try it
?
|
<🎜>header("Access-Control-Allow-Origin:*");<🎜>
<🎜>$url = 'http://'.$_SERVER['HTTP_HOST'];<🎜>
<🎜>$file = (isset($_POST["file"])) ? $_POST["file"] : '';<🎜>
<🎜>if($file)<🎜>
<🎜>{<🎜>
<🎜>$data = base64_decode(str_replace('data:image/png;base64,', '', $file)); //The screenshot can only get png format pictures, so just process the png.<🎜>
<🎜>$name = md5(time()) . '.png'; // The file name is md5 processed here<🎜>
<🎜>file_put_contents($name, $data);<🎜>
<🎜>echo "$url/$name";<🎜>
<🎜>die;<🎜>
<🎜>}<🎜>
<🎜>?>
|
The above is the entire content of this article, I hope you all like it.