Last active
December 12, 2015 09:09
-
-
Save arikui/4749240 to your computer and use it in GitHub Desktop.
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 | |
header("content-type: text/plain"); | |
$images_dir_name = "images"; | |
$ip = $_SERVER["REMOTE_ADDR"]; | |
// machine dirs | |
$script_filedir = dirname($_SERVER["SCRIPT_FILENAME"]); | |
$image_save_dir = $images_dir_name . DIRECTORY_SEPARATOR . $ip; | |
define("SAVE_DIR", $script_filedir . DIRECTORY_SEPARATOR . $image_save_dir); | |
// http resources | |
$upload_dir = dirname($_SERVER["REQUEST_URI"]); | |
$image_res_dir = $images_dir_name . "/" . $ip; | |
$host = $_SERVER["HTTP_HOST"]; | |
define("RESOURCE_DIR", "http://" . $host . $upload_dir . "/" . $image_res_dir); | |
function get_filename(){ | |
if(!file_exists(SAVE_DIR)){ | |
mkdir(SAVE_DIR); | |
} | |
$tempname = tempnam(SAVE_DIR, "i"); | |
return basename($tempname, ".tmp") . ".png"; | |
} | |
function upload($tmp_file){ | |
$filename = get_filename(); | |
$new_path = SAVE_DIR . DIRECTORY_SEPARATOR . $filename; | |
if(move_uploaded_file($tmp_file, $new_path)){ | |
return RESOURCE_DIR . "/" . $filename; | |
} | |
throw new Exception("failed to upload."); | |
} | |
echo upload($_FILES["imagedata"]["tmp_name"]); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment