4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2024-05-01

インストール

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

動作確認

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'
})

コマンドライン

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"))

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

echo 'Options +Indexes' > .htaccess

デモ

See the Pen upload.php by John Doe (@04) on CodePen.

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'
})

コマンドライン

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"))
4
9
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
4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?