0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HttpPostでファイル受信(PHP)

0
Posted at

はじめに

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

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

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

リファレンス

[POST メソッドによるアップロード]
(https://www.php.net/manual/ja/features.file-upload.post-method.php)
[エラーメッセージの説明]
(https://www.php.net/manual/ja/features.file-upload.errors.php)

アップロード用の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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?