LoginSignup
0
0

More than 3 years have passed since last update.

HttpPostでファイル受信(PHP)

Posted at

はじめに

XAMPPのApacheを利用して、ファイル受信をしたかった。
コピペでPHPのファイル受信プログラムを作成したが、ファイルを受け取れず困ったのでメモ。

エラーコードを見ると1が返されており、ファイルサイズが大きすぎたことが原因だとわかった。
php.ini の upload_max_filesizeを書き換えて再起動することによって解決した。

送信側については書かない。

リファレンス

POST メソッドによるアップロード
エラーメッセージの説明

アップロード用のphpファイル

upload.php
<?php
//情報取得
$name = $_FILES["file"]["name"]; // ファイル名
$mimetype = $_FILES["file"]["type"]; // Content-Type
$filesize = $_FILES["file"]["size"]; // ファイルサイズ
$tmpname = $_FILES["file"]["tmp_name"]; // 一時ファイル名
$error = $_FILES["file"]["error"]; // エラー

//ログ
$file = 'log.txt';
$current = file_get_contents($file);
$current .= $name . "\n";
$current .= $mimetype . "\n";
$current .= $filesize . "\n";
$current .= $tmpname . "\n";
$current .= $error . "\n";
file_put_contents($file, $current);

//なければフォルダを作成
$target_dir = "imgs";
if(!file_exists($target_dir))
{
mkdir($target_dir, 0777, true);
}

//保存
$filename = $target_dir . "/" . $name;
$result = @move_uploaded_file( $tmpname, $filename );

echo( $result );
?>
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