Created
April 23, 2014 17:03
-
-
Save yagihash/11223679 to your computer and use it in GitHub Desktop.
ファイルチェックコードのサンプル
This file contains hidden or 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 | |
$file = $_FILES["file"]; | |
$finfo = new finfo(FILEINFO_MIME_TYPE); | |
$type = $finfo -> file($file['tmp_name']); | |
if (!isset($file['error']) or !is_int($file['error'])) { | |
throw new Exception("An error occured in file uploading."); | |
} else if (!preg_match("/^application\/pdf/", $type)) { | |
throw new Exception("Only pdf file can be accepted."); | |
} else if ($file['size'] > 1000000) { | |
throw new Exception("Uploaded file is too large."); | |
} else { | |
if (move_uploaded_file($file["tmp_name"], ($file_path = "./files/" . bin2hex(openssl_random_pseudo_bytes(16)) . ".pdf"))) { | |
chmod($file_path, 0644); | |
$file_name = basename($file_path); | |
} else { | |
throw new Exception("An error occured in saving file."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment