PHPでファイルをアップロードする 画像アップロード

# インストール

```bash
wget https://gist.githubusercontent.com/GitHub30/d6960b85c73e6e5f20386fa665873183/raw/upload.php
```

https://gist.github.com/GitHub30/d6960b85c73e6e5f20386fa665873183


# 動作確認

## JavaScript

```javascript
var fd = new FormData()
fd.append('file', new File(['a'], 'a.txt'))
await fetch('http://localhost/upload.php', {
   method: 'POST',
   body: fd
})

var fd = new FormData()
fd.append('file[]', new File(['b'], 'b.txt'))
fd.append('file[]', new File(['c'], 'c.txt'))
await fetch('http://localhost/upload.php', {
   method: 'POST',
   body: fd
})

await fetch('http://localhost/upload.php?name=hoge.txt', {
   method: 'POST',
   body: 'hoge'
})
await fetch('http://localhost/upload.php', {
   method: 'PUT',
   body: 'fuga'
})
```

## コマンドライン

```bash
curl -T image.png http://localhost/upload.php?name=image.png
curl -F file='@image.png' http://localhost/upload.php
curl -F file='@image.png' http://localhost/upload.php?folder=foo
curl -F file='@image.png' http://localhost/upload.php?folder

# For Windows
curl.exe -T image.png http://localhost/upload.php?name=image.png
curl.exe -F file='@image.png' http://localhost/upload.php
curl.exe -F file='@image.png' http://localhost/upload.php?folder=foo
curl.exe -F file='@image.png' http://localhost/upload.php?folder
# or
del alias:curl
curl -T image.png http://localhost/upload.php?name=image.png
curl -F file='@image.png' http://localhost/upload.php
curl -F file='@image.png' http://localhost/upload.php?folder=foo
curl -F file='@image.png' http://localhost/upload.php?folder
# or
curl http://localhost/upload.php?name=image.png -Method POST -InFile image.png -ContentType image/png
Add-Type -AssemblyName System.Web
curl http://localhost/upload.php?name=image.png -Method POST -InFile image.png -ContentType ([System.Web.MimeMapping]::GetMimeMapping("image.png"))
```

# ディレクトリ内のファイル一覧を表示

```bash
echo 'Options +Indexes' > .htaccess
```

# デモ

https://kix51.000webhostapp.com

<p class="codepen" data-height="600" data-default-tab="result" data-slug-hash="Pogvaoz" data-user="04" style="height: 600px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
  <span>See the Pen <a href="https://codepen.io/04/pen/Pogvaoz">
  upload.php</a> by John Doe (<a href="https://codepen.io/04">@04</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>


## JavaScript

```javascript
var fd = new FormData()
fd.append('file', new File(['a'], 'a.txt'))
await fetch('https://kix51.000webhostapp.com/upload.php', {
   method: 'POST',
   body: fd
})

var fd = new FormData()
fd.append('file[]', new File(['b'], 'b.txt'))
fd.append('file[]', new File(['c'], 'c.txt'))
await fetch('https://kix51.000webhostapp.com/upload.php', {
   method: 'POST',
   body: fd
})

await fetch('https://kix51.000webhostapp.com/upload.php?name=hoge.txt', {
   method: 'POST',
   body: 'hoge'
})
await fetch('https://kix51.000webhostapp.com/upload.php', {
   method: 'POST',
   body: 'fuga'
})
```

## コマンドライン

```bash
curl -T image.png https://kix51.000webhostapp.com/upload.php?name=image.png
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder=foo
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder

# For Windows
curl.exe -T image.png https://kix51.000webhostapp.com/upload.php?name=image.png
curl.exe -F file='@image.png' https://kix51.000webhostapp.com/upload.php
curl.exe -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder=foo
curl.exe -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder
# or
del alias:curl
curl -T image.png https://kix51.000webhostapp.com/upload.php?name=image.png
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder=foo
curl -F file='@image.png' https://kix51.000webhostapp.com/upload.php?folder
# or
curl https://kix51.000webhostapp.com/upload.php?name=image.png -Method POST -InFile image.png -ContentType image/png
Add-Type -AssemblyName System.Web
curl https://kix51.000webhostapp.com/upload.php?name=image.png -Method POST -InFile image.png -ContentType ([System.Web.MimeMapping]::GetMimeMapping("image.png"))
```