post.php

来自「applet在線上繪圖,允許在瀏覽器上繪圖,最後儲存成各式的圖檔」· PHP 代码 · 共 47 行

PHP
47
字号
<?php
/*--------------------------------------------------------------------
This script accepts the image data from the posted variable 'image'.
Then it decodes the image data from BASE64 form to binary array and saves it to a disk file.
Afterward it asks the applet to redirect the browser to the 'view.php'
to prove that the image is saved successfully.
---------------------------------------------------------------------*/

if (isset($_POST['image'])) {

    // The script saves the image to the current directory
    // with the file name suggested by the editor,
    $filename = $_POST['image_name'];

    // But you can use any desirable filename, for example
    // create a unique filename using the current datetime
    // and the user's IP address:
    // $filename = time().'-'.str_replace('.','-',$_SERVER['REMOTE_ADDR']).".png";
    	
    if (is_writable("./")) { // can I write in this directory ?
    	
    	if ($fp = fopen($filename,"wb")) {
	    // convert the image data from BASE64 to binary form
    	    $bin_image = base64_decode($_POST['image']);

    	    // write to the file
    	    fwrite($fp,$bin_image);
    	    fclose($fp);
    	    
    	    // Redirect the browser to the 'view.php'.
    	    // The file name and it's MIME type
    	    // are passed to the 'view.php' as URL prameters
    	    
    	    $filename   = urlencode($filename);
    	    $image_type = urlencode($_POST['image_type']);    	    
   	    echo "#SHOWDOCUMENT=view.php?filename=$filename&image_type=$image_type\n";
    	}
    	else
    	    echo("#SHOWMESSAGE=Failed to open $filename for writing\n");
    }
    else
	echo("#SHOWMESSAGE=post.php hasn't write permissions in this directory on the server\n");
}
else
    echo("#SHOWSTATUS=Image data are not found\n");

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?