<?php if (isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])){ // 各ファイルをチェック foreach ($_FILES['upfile']['error'] as $k => $error){ try { if (!is_int($error)){ throw new RuntimeException("[{$k}] パラメータが不正です"); } // $_FILES['upfile']['error']の値を確認 switch ($_FILES['upfile']['error']){ case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new RuntimeException('ファイルが選択されていません'); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: // フォーム定義の最大サイズ超過 throw new RuntimeException('ファイルサイズが大きすぎます'); default: throw new RuntimeException('その他のエラーが発生しました'); } // $_FILES['upfile']['mime']の値はブラウザ側で偽装可能なので // MIMEタイプを自前でチェックする if (!$info @getimagesize($_FILES['upfile']['tmp_name'][$k])){ throw new RuntimeException("[{$k}] 有効な画像ファイルを指定してください"); } if (!in_array($info[2], [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG], true)){ throw new RuntimeException("[{$k}] 未対応の画像形式です"); } // 画像処理に使う関数名を決定する $create = str_replace('/', 'createfrom', $info['mime']); $output = str_replace('/', '', $info['mime']); // 縦横比を維持したまま、120*120 以下に収まるサイズを求める if ($info[0] >= $info[1]){ $dst_w = 120; $dst_h = ceil(120 * $info[1] / max($info[0], 1)); } else { $dst_w = ceil(120 * $info[0] / max($info[1], 1)); $dst_h = 120; } // 元画像リソースの生成を試みる if (!$src = @$create($_FILES['upfile']['tmp_name'][$k])){ throw new RuntimeException("[{$k}] 画像リソースの生成に失敗しました"); } // リサンプリング先画像リソースを生成する $dst = imagecreatetruecolor($dst_w, $dst_h); // getimagesize関数で得られた情報も利用してリサンプリングを行う imagecopyresampled($dst, $src, 0, 0, 0, 0, $dst_w, $dst_h, $info[0], $info[1]); // ファイルデータからSHA-1ハッシュを取ってファイル名を決定し、保存する if (!$output( $dst, sprintf('./resized/%s%s', sha1_file($_FILES['upfile']['tmp_name'][$k]), image_type_to_extension($info[2]) ) )) { throw new RuntimeException("[{$k}] ファイル保存時にエラーが発生しました"); } $msgs[] = ['green', "[{$k}] リサイズして保存しました"]; } catch (RuntimeException $e){ $msgs = ['red', $e->getMessage()]; } if (isset($img) && is_resource($img)){ imagedestroy($img); } if (isset($dst) && is_resource($dst)){ imagedestroy($dst); } } } // XTHMLとしてブラウザに認識させる // (IE8以外はサポート対象外) header('Content-Type: application/xhtml+xml; charset=utf-8'); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>画像アップロード</title> </head> <body> <?php if (!empty($msgs)): ?> <ul> <?php foreach ($msgs as $msg): ?> <li style="color:<?=$msg[0]?>;"><?= htmlspecialchars($msg[1], ENT_QUOTES, 'UTF-8') ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>画像ファイルを選択(GIF, JPEG, PNGのみ対応)</legend> <ul> <?php for ($i = 0; $i < 10; $i++): ?> <li><input type="file" name="upfile[]" /></li> <?php endfor; ?> </ul> <input type="submit" value="送信" /> </fieldset> </form> </body> </html>
imagecreatefromstring
imagecreatefromstring
Create a new image from the image stream in the string
<?php if (isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])){ try { // $_FILES['upfile']['error']の値を確認 switch ($_FILES['upfile']['error']){ case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new RuntimeException('ファイルが選択されていません'); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: // フォーム定義の最大サイズ超過 throw new RuntimeException('ファイルサイズが大きすぎます'); default: throw new RuntimeException('その他のエラーが発生しました'); } // GD画像リソースの生成を試みる if (!$img = @imagecreatefromstring(file_get_contents($_FILES['upfile']['tmp_name']))){ throw new RuntimeException('有効な画像ファイルを指定してください'); } // ファイルデータからSHA-1ハッシュを取ってファイル名を決定し、保存する if (!imagepng($img, sprintf('./uploads/%s.png', sha1_file($_FILES['upfile']['tmp_name'])))){ throw new RuntimeException('ファイル保存時にエラーが発生しました'); } $msg = ['green', 'ファイルは正常にアップロードされました']; } catch (RuntimeException $e){ $msg = ['red', $e->getMessage()]; } if (isset($img) && is_resource($img)){ imagedestroy($img); } } // XTHMLとしてブラウザに認識させる // (IE8以外はサポート対象外) header('Content-Type: application/xhtml+xml; charset=utf-8'); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>画像アップロード</title> </head> <body> <?php if (isset($msg)): ?> <fieldset> <legend>結果</legend> <span style="color:<?=$msg[0]?>;"><?=$msg[1]?></span> </fieldset> <?php endif; ?> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>画像ファイルを選択</legend> <input type="file" name="upfile" /><br /> <input type="submit" value="送信" /> </fieldset> </form> </body> </html>
exif_imagetype
is_int
Find whether the type of a variable is integer
$_FILES[”][‘error’]
PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP.
UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION
exif_imagetype
Determine the type of an image
IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG
sprintf
Return a formatted string
image_type_to_extension
Get file extension for image type
<?php if (isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])){ try { // $_FILES['upfile']['error']の値を確認 switch ($_FILES['upfile']['error']){ case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new RuntimeException('ファイルが選択されていません'); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: // フォーム定義の最大サイズ超過 throw new RuntimeException('ファイルサイズが大きすぎます'); default: throw new RuntimeException('その他のエラーが発生しました'); } // $_FILES['upfile']['mime']の値はブラウザ側で偽装可能なので、MIMEタイプを自前でチェック $type = @exif_imagetype($_FILES['upfile']['tmp_name']); if(!in_array($type, [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG], true)) { throw new RuntimeException('画像形式が未対応です'); } // ファイルデータからSHA-1ハッシュを取ってファイル名を決定し、ファイルを保存する $path = sprintf('./img/%s%s', sha1_file($_FILES['upfile']['tmp_name']), image_type_to_extension($type)); if (!move_uploaded_file($_FILES['upfile']['tmp_name'], $path)){ throw new RuntimeException('ファイル保存時にエラーが発生しました'); } chmod($path, 0644); $msg = ['green', 'ファイルは正常にアップロードされました']; } catch (RuntimeException $e){ $msg = ['red', $e->getMessage()]; } } // XTHMLとしてブラウザに認識させる // (IE8以外はサポート対象外) header('Content-Type: application/xhtml+xml; charset=utf-8'); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>画像アップロード</title> </head> <body> <?php if (isset($msg)): ?> <fieldset> <legend>結果</legend> <span style="color:<?=$msg[0]?>;"><?=$msg[1]?></span> </fieldset> <?php endif; ?> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>画像ファイルを選択(GIF, JPEG, PNGのみ対応)</legend> <input type="file" name="upfile" /><br /> <input type="submit" value="送信" /> </fieldset> </form> </body> </html>
move_uploaded_file()
bool move_uploaded_file ( string $filename , string $destination )
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP’s HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.
<?php if($_FILES['file']){ move_uploaded_file($_FILES['file']['tmp_name'], './img/test.jpg'); } ?> <form action="./index.php" method="POST" enctype="multipart/form-data"> <input type="file" name="faile"> <input type="submit" value="ファイルをアップロードする"> </form>
php error
<?php try{ if(is_uploaded_file($_FILES['file']['tmp_name'])){ move_uploaded_file($_FILES['file']['tmp_name'], './img/test.jpg'); } } catch(Exception $e){ echo 'エラー:', $e->getMessage().PHP_EOL; } ?> <form action="./index.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="ファイルをアップロードする"> </form>
php form
php.init
file_uploads, post_max_size, upload_max_filesize, memory_limit, max_execution_time, upload_tmp_dir, max_file_uploads
Tells whether the file was uploaded via HTTP POST
what’s going on?
this function mean a*b, z is x time multiply.
def naive(a, b): x = a; y = b z = 0 while x > 0: z = z + y x = x - 1 return z print(naive(4, 5))
def russian(a, b): x = a; y = b z = 0 while x > 0: if x % 2 == 1: z = z + y y = y << 1 x = x >> 1 return z print(russian(5, 5))
remote add origin to GitHub
[vagrant@localhost asteroids]$ git remote [vagrant@localhost asteroids]$ git remote add origin git@github.com/hoge/test.git [vagrant@localhost asteroids]$ git remote origin [vagrant@localhost asteroids]$ git remote -v origin git@github.com/hoge/test.git (fetch) origin git@github.com/hoge/test.git (push)
get change from remote repository, same as git fetch.
[vagrant@localhost reflections]$ git pull origin master
さくらレンタルサーバーでのgit管理
バージョン管理したいフォルダに移動
% git init % git status $ git add . (全てのファイルをインデックスへ追加) $ git commit -m "2016/12/11 commit" (コメントは任意)
リモートリポジトリの作成
% mkdir git % cd git % ls % mkdir remote.git % git init --bare
リモートリポジトリへpush
% git remote add origin ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git % git remote -v origin ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git (fetch) origin ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git (push) % git push origin master:master
ログ
% git log
git diff
% git diff
commit logの閲覧終了:q
git checkout
hidden file you can find
ls -a
git diff
git diff –staged
git diff commit1 commit2
git branch
-git branch
-git branch easy-mode
[vagrant@localhost reflections]$ git branch * master [vagrant@localhost reflections]$ git branch easy-mode [vagrant@localhost reflections]$ git branch easy-mode * master
checkout
[vagrant@localhost reflections]$ git checkout easy-mode Switched to branch 'easy-mode' [vagrant@localhost reflections]$ git branch * easy-mode master
git log –graph –oneline
git merge
[vagrant@localhost reflections]$ git merge master easy-mode Updating d878411..bdd001b Fast-forward asteroids/game.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
remote branchへのpush
git push origin master
diff command
mac os use this command
[vagrant@localhost git]$ diff -u game_new.js game_old.js --- game_new.js 2016-12-11 08:17:10.604013030 +0900 +++ game_old.js 2016-12-11 08:17:10.605013028 +0900 @@ -4,9 +4,9 @@ // KEY_CODES = { - 13: 'enter', 32: 'space', 37: 'left', + 38: 'up', 39: 'right', 40: 'down', 70: 'f', @@ -392,7 +392,7 @@ this.vel.rot = 0; } - if (KEY_STATUS.spacr) { + if (KEY_STATUS.up) { var rad = ((this.rot-90) * Math.PI)/180; this.acc.x = 0.5 * Math.cos(rad); this.acc.y = 0.5 * Math.sin(rad); @@ -406,7 +406,7 @@ if (this.delayBeforeBullet > 0) { this.delayBeforeBullet -= delta; } - if (KEY_STATUS.enter) { + if (KEY_STATUS.space) { if (this.delayBeforeBullet <= 0) { this.delayBeforeBullet = 10; for (var i = 0; i < this.bullets.length; i++) { @@ -919,7 +919,7 @@ waiting: function () { Text.renderText(ipad ? 'Touch Sreen to Start' : 'Press Space to Start', 36, Game.canvasWidth/2 - 270, Game.canvasHeight/2); if (KEY_STATUS.space || window.gameStart) { - KEY_STATUS.space = false; // hack so we don't move right away + KEY_STATUS.space = false; // hack so we don't shoot right away window.gameStart = false; this.state = 'start'; }
Port connection
22 for ssh
80 for http
The port range that a normal (non-root) user can listen on is 1024 through 65535.
Hosts:a machine on the internet that might host services
vagrant@vagrant-ubuntu-trusty-64:~$ ping -c 1 google.com PING google.com (172.217.24.142) 56(84) bytes of data. 64 bytes from nrt20s01-in-f14.1e100.net (172.217.24.142): icmp_seq=1 ttl=50 time=87.3 ms
vagrant@vagrant-ubuntu-trusty-64:~$ host google.com google.com has address 172.217.25.238 google.com has IPv6 address 2404:6800:4004:817::200e google.com mail is handled by 10 aspmx.l.google.com. google.com mail is handled by 30 alt2.aspmx.l.google.com. google.com mail is handled by 50 alt4.aspmx.l.google.com. google.com mail is handled by 40 alt3.aspmx.l.google.com. google.com mail is handled by 20 alt1.aspmx.l.google.com. vagrant@vagrant-ubuntu-trusty-64:~$ host -t a google.com google.com has address 172.217.25.238
vagrant@vagrant-ubuntu-trusty-64:~$ dig www.udacity.com ; <<>> DiG 9.9.5-3ubuntu0.10-Ubuntu <<>> www.udacity.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42131 ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.udacity.com. IN A ;; ANSWER SECTION: www.udacity.com. 3081 IN CNAME apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 54.186.129.122 apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 52.26.227.216 apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 35.163.216.178 ;; Query time: 86 msec ;; SERVER: 10.0.2.3#53(10.0.2.3) ;; WHEN: Sat Dec 10 14:32:18 UTC 2016 ;; MSG SIZE rcvd: 160
CNAMEレコード 【 Canonical NAME record 】 CNAMEレコードとは、DNSで定義されるそのドメインについての情報の種類の一つで、あるドメイン名やホスト名の別名を定義するもの。
ドメイン名に対応するIPv6形式のIPアドレスが書いてある行が「AAAAレコード」
NSレコードは、ゾーン情報を管理するネームサーバー(DNSサーバー)名を定義するレコード
network address
vagrant@vagrant-ubuntu-trusty-64:~$ ip addr show 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:6c:cb:39 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe6c:cb39/64 scope link valid_lft forever preferred_lft forever
my ip address
http://test-ipv6.com/
what server they use
nc send string over network.
vagrant@vagrant-ubuntu-trusty-64:~$ printf ‘HEAD / HTTP/1.1\r\nHost: www.google.co.jp\r\n\r\n’ | nc www.google.co.jp 80
HTTP/1.1 200 OK
Date: Sat, 10 Dec 2016 13:39:24 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Shift_JIS
P3P: CP=”This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.”
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=91=vO7aa7YjWxJPGDWffZj–p855KgcdJdpvH9dCUAVgWBX7f8JZdeweK_wY3msj09u8Osyz2tK4H4X3cjnZX1QK45cCxRB910amTq7Kmllk6TS3iH9HMIVw73LAaOW4Swq; expires=Sun, 11-Jun-2017 13:39:24 GMT; path=/; domain=.google.co.jp; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
vagrant@vagrant-ubuntu-trusty-64:~$ printf ‘HEAD / HTTP/1.1\r\nHost: www.amazon.com\r\n\r\n’ | nc www.amazon.com 80
HTTP/1.1 301 Moved Permanently
Date: Sat, 10 Dec 2016 13:44:34 GMT
Server: Server
Location: https://www.amazon.com/
Content-Type: text/html; charset=iso-8859-1
vagrant@vagrant-ubuntu-trusty-64:~$ man nc
connecting by nc command.
vagrant@vagrant-ubuntu-trusty-64:~$ nc localhost 3456 vagrant@vagrant-ubuntu-trusty-64:~$ nc -l 3456