2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】 ファイル操作

Last updated at Posted at 2019-11-23

PHPのファイル操作に関する備忘録です。

1.html(input)からPHP側に送付するフォームを作る

<form action="image_form.php" method="post" enctype="multipart/form-data">
<input type="file" name="img">
<input type="submit" value="アップロードする">
</form>

2.PHP側の処理

<?php

//ファイル名を取得する 例)*****.pngとか
$filename = $_FILES['img']['name'];

//一時保存されたファイルのパスとファイルを取得
$filedata = $_FILES['img']['tmp_name'];

//ファイルを保存するフォルダ名
$storeDir = '/home/vagrant/image/img/';

//一時保存したファイルを指定のファイルに移動する.
//第一引数には一時保存したファイルパス
//第二引数には保存先のファイルパス
move_uploaded_file($filedata,$storeDir.$filename);

?>

解説

$_FILESについて

上記ではファイルを送信するnameをimgとしました。
ですので、$_FILES['img']をvar_dump($_FILES['img'])すると下記のように出力します。

array(5) { 
["name"]=> string(51) "スクリーンショット 2019-11-16 16.07.59.png" 
["type"]=> string(9) "image/png" 
["tmp_name"]=> string(14) "/tmp/php8VMhWj" 
["error"]=> int(0) 
["size"]=> int(29001) 
}

name:アップロードしたファイルの名前
type:アップロードしたファイルの種類
tmp_name:一時保存しているファイルのパス
error:エラーが発生していると1,エラーなしは0
size:ファイルサイズを表示(単位はbyte)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?