0
0

More than 3 years have passed since last update.

pythonで画像を送信、phpで保存する

Posted at

色々わr・・・面白いことに使えそうだからメモしとく

pic_post.py
import requests
#こちら送信側→ローカルに置く
with open("img.jpg", "rb") as f:#画像をopen(読み込みみたいな?)する
    url='http://hogehoge.jp/pic_save.php'#送り先
    r = requests.post(url, f.read())#送信
pic_save.php
<?php
//こちら受信側→サーバ側に置いておく
    $bin = file_get_contents("php://input");//受信したモノを取り出し
    file_put_contents('./img.jpg',$bin);//同じディレクトリに『img.jpg』というファイル名で保存する
?>

とりあえずは上のコードで動くはず
f.read()の意味は何だよとかは追記するかも
拡張子が違う場合(png、gif)やら画像を選びたい場合やらとかも本当は考えたいところ

参考

[PHP]画像をダウンロードしてサーバーの特定のディレクトリに保存する
http://codaholic.org/?p=341

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