LoginSignup
5
10

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

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

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
5
10
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
5
10