LoginSignup
1
1

More than 5 years have passed since last update.

[PHP]画像アップロード機能のformのテンプレ

Last updated at Posted at 2018-12-24
<form action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php h(MAX_FILE_SIZE); ?>">
    <input type="file" name="image">
    <input type="submit" name="upload">
</form>

htmlは上記のようにしつつ


<?php
define('MAX_FILE_SIZE', 1 * 1024 * 1024); //1MB
?>

php部分には、このように書いておく。
これで、「MAX_FILE_SIZE」が「1*1024*1024」の1MBだと規定している。


<form action="" method="post" enctype="multipart/form-data">

これはおまじないとのこと。

<input type="hidden" name="MAX_FILE_SIZE" value="<?php h(MAX_FILE_SIZE); ?>">

valueで、ファイルサイズの上限サイズを指定している。
上の2つめのコードから、「MAX_FILE_SIZE」が1MBと規定している。

また、formの一番上に記述していることに注目。
一番上に書くことによって、アップロードを待ってからサイズオーバーでエラーが出ることを防いでくれている。

参照:http://doremi.s206.xrea.com/php/tips/upload.html

1
1
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
1
1