お世話になっております。
兼ねてより制作中のWEBサイトで画像の保管をする際に使用している
move_uploaded_file()で下記エラーの解消がしきれず困っております。
ErrorMessage
1Warning (2): move_uploaded_file(webroot/files/Ices/image_file/20200103113922DSC_0002.JPG): failed to open stream: No such file or directory [APP/Controller/IcesController.php, line 119] 2Warning (2): move_uploaded_file() [<a href='https://secure.php.net/function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/php6xMSmE' to 'webroot/files/Ices/image_file/20200103113922DSC_0002.JPG' [APP/Controller/IcesController.php, line 119] 3Warning (512): Unable to emit headers. Headers sent in file=/home/ec2-user/environment/my_pt_lesson/vendor/cakephp/cakephp/src/Error/Debugger.php line=853 [CORE/src/Http/ResponseEmitter.php, line 51] 4Warning (2): Cannot modify header information - headers already sent by (output started at /home/ec2-user/environment/my_pt_lesson/vendor/cakephp/cakephp/src/Error/Debugger.php:853) [CORE/src/Http/ResponseEmitter.php, line 152] 5Warning (2): Cannot modify header information - headers already sent by (output started at /home/ec2-user/environment/my_pt_lesson/vendor/cakephp/cakephp/src/Error/Debugger.php:853) [CORE/src/Http/ResponseEmitter.php, line 185] 6Warning (2): Cannot modify header information - headers already sent by (output started at /home/ec2-user/environment/my_pt_lesson/vendor/cakephp/cakephp/src/Error/Debugger.php:853) [CORE/src/Http/ResponseEmitter.php, line 185] 7Warning (2): Cannot modify header information - headers already sent by (output started at /home/ec2-user/environment/my_pt_lesson/vendor/cakephp/cakephp/src/Error/Debugger.php:853) [CORE/src/Http/ResponseEmitter.php, line 185]
▽エラー発生時に取得できている内容(一部抜粋です)▽
※line 119の取得内容の一部
line119ofProcessB
1$file = [ 2 'tmp_name' => '/tmp/php6xMSmE', 3 'error' => (int) 0, 4 'name' => 'DSC_0002.JPG', 5 'type' => 'image/jpeg', 6 'size' => (int) 170153 7] 8$filePath = 'webroot/files/Ices/image_file/20200103113922DSC_0002.JPG' 9$size = (int) 170153 10$ext = 'image/jpeg'
上記情報取得時のコード(下記IcesController全体のコード中"処理B"の部分)
PHP
1 move_uploaded_file($_FILES['image_file']['tmp_name'], $filePath); 2 ※↑line119がここになります
エラーメッセージの内容から、本サイト、他のネット記事を参考に、
"パスの指定方法の誤り"が大方の原因であることはある程度理解できたため、
自身の環境にあわせ指定をしたところ、一旦は解決できました(パスの記載内容は丁度この内容で解決してました)。
ただ、その後、少し時間をあけてから同様の操作をすると、
同じエラーが出てしまいます。その根本の解決策の模索中です。
▽環境▽
AWS Cloud9:無料枠
MySQL:ver5.7.26
CakePHP:ver3.8.2
PHP:ver7.2.19
※以前の質問記事(https://teratail.com/questions/230485)から大きく変更した点
A:画像投稿はmove_uploaded_file()で処理(今回はこの部分についてのご相談です)
B:以前使用していたプラグイン"FriendsOfCake/cakephp-upload"は無効化
C:画像データ自体はAmazon S3へ保管(他はMySQLへ)
▽"AWS Cloud9:無料枠"のディレクトリ構成です▽
※"image_fileフォルダ"の直下に画像データが入っていました。
※move_uploaded_file()を使用してから下記ディレクトリ構成に変更はありません。
▽画像登録時のコード全体像(IcesController)▽
PHP
1public function add() 2 { 3 $ice = $this->Ices->newEntity(); 4 if ($this->request->is('post')) { 5 $ice = $this->Ices->patchEntity($ice, $this->request->getData()); 6 $file = $this->request->getData('image_file'); 7 $filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name']; 8 //特定の拡張子かつ800KB未満でないとアップできないようにする 9 $size = $file['size'];//容量を取得 10 $ext = $file['type'];//拡張子を取得 11 if($ext ==='image/jpg' && $size < 819200)//処理A 12 { 13 $file = $this->request->getData('image_file'); 14 $filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name']; 15 move_uploaded_file($_FILES['image_file']['tmp_name'], $filePath);//Storage to directry to photo data. 16 }elseif($ext ==='image/jpeg' && $size < 819200){//処理B 17 $file = $this->request->getData('image_file'); 18 $filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name']; 19 move_uploaded_file($_FILES['image_file']['tmp_name'], $filePath); 20 }elseif($ext ==='image/png' && $size < 819200){//処理C 21 $file = $this->request->getData('image_file'); 22 $filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name']; 23 move_uploaded_file($_FILES['image_file']['tmp_name'], $filePath); 24 }elseif($ext ==='image/gif' && $size < 819200){//処理D 25 $file = $this->request->getData('image_file'); 26 $filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name']; 27 move_uploaded_file($_FILES['image_file']['tmp_name'], $filePath); 28 }else{//処理E 29 $this->Flash->error(__('種類はjpg,jpeg,png,gif,サイズは800KBまでで登録をお願いします')); 30 return $this->redirect(['controller' => 'ices', 'action' => 'add']); 31 } 32 $data = array( 33 'manufacturer' => $this->request->getData('manufacturer'), 34 'ice_name' => $this->request->getData('ice_name'), 35 'ice_fraver' => $this->request->getData('ice_fraver'), 36 'price_no_tax' => $this->request->getData('price_no_tax'), 37 'buy_year' => $this->request->getData('buy_year'), 38 'buy_month' => $this->request->getData('buy_month'), 39 'simple_comment' => $this->request->getData('simple_comment'), 40 'desc_comment' => $this->request->getData('desc_comment'), 41 'image_file' => date("YmdHis") . $file['name'], 42 'repeat_rate' => $this->request->getData('repeat_rate'), 43 'stock_rate' => $this->request->getData('stock_rate'), 44 ); 45 $ice = $this->Ices->newEntity($data); 46 $ice->user_id = $this->Auth->user()['id']; 47 if ($this->Ices->save($ice)) {//Storage to DB 48 //to AWSs3[upload] 49 // if(isset($ice)){ 50 // $this->setAction('upload',$ice); 51 // }; 52 $this->Flash->success(__('アイスの登録ができました'),['key' => 'positive']); 53 return $this->redirect(['controller' => 'users', 'action' => 'view', $this->Auth->user()['id']]); 54 } 55 $this->Flash->error(__('アイスの登録ができませんでした')); 56 } 57 //一部略 58}
・アップロード対象の画像ファイルの拡張子、容量を元に処理A~Eに分けられるように記述し、
アップロードができるようになるまでに使用したファイルが"処理B"に振られたため、
そこでパスの調整をし、できるようになったのが、下記の記載でした。
$filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name'];
・その後、他の想定ファイルでもアップできることを確認し、
記載内容や作業環境の整理の一環とし、下記の作業を行っています。
1、//debug();や、他の不要なコメントアウトの削除。
2、不要な空行をCtrl+Deleteで詰める
3、image_fileに収納された画像データの削除
4、phpMyAdminより検証で増えたレコードを一括削除
5、$filePath = 'webroot/files/Ices/image_file/'.date("YmdHis").$file['name'];を処理A~Eまでに記述
...といった内容ですが、どこか今回のエラーに繋がる要因があるのでしょうか。
また、そもそものパスの指定方法自体も、改めて勉強中ですが、
画像投稿処理ができた時点のコードをそのまま保存し、
翌日に試しても同様のエラーが出る、といった状況がまばらに続いており、
困惑しているところもありますが、何か解決の糸口となるご提示を
していただけると非常に助かります。
お手数おかけしますが、どうぞご教示をお願いいたします。
▼以下、2020/1/4 追記分です▼
1/3にいただいたコメントより、公式ドキュメント
(https://book.cakephp.org/3/ja/core-libraries/file-folder.html)を
元に上記質問内容内"IcesController.php"の"//処理B"内を
下記のように書き換え実行すると、"image_file"ディレクトリの直下にファイルが
保存されることまでは確認ができました。
ただ、その"保存されるファイル"というのが、"size"が"0"となっており、
画像ファイルとして閲覧することはできない状態で保存されています。
---以下、書き換えた内容---
▽IcesController.phpの処理B内
php
1$time = Time::now('Asia/Tokyo'); //時刻をTokyoに指定し 2$time = $time->i18nFormat('yyyyMMddHHmmss');//時間取得 3$file = $this->request->getData('image_file'); //★A:ファイル情報取得 4$file = new File(WWW_ROOT.'/files/Ices/image_file/'.$time.$file['name']); 5$file->create();//★B:image_fileディレクトリへ新規ファイルとして画像ファイル作成
この書き換えた内容の★Aのすぐ下でdebug($file);をした場合、
sizeは投稿しようとするファイルの容量を表示しています
[ 'tmp_name' => '/tmp/phplCeijU', 'error' => (int) 0, 'name' => 'DSC_0002.JPG', 'type' => 'image/jpeg', 'size' => (int) 170153 ]
また、★Bの下でdebug($file);してみると下記のように表示されています。
object(Cake\Filesystem\File) { Folder => object(Cake\Filesystem\Folder) { path => '/home/ec2-user/environment/my_pt_lesson/webroot/files/Ices/image_file' sort => false mode => (int) 493 [protected] _fsorts => [ 'name' => 'getPathname', 'time' => 'getCTime' ] [protected] _messages => [] [protected] _errors => [] [protected] _directories => null [protected] _files => null } name => '20200104130459DSC_0002.JPG' info => [] handle => null lock => null path => '/home/ec2-user/environment/my_pt_lesson/webroot/files/Ices/image_file/20200104130459DSC_0002.JPG' }
今の時点で容量も含めての保存処理が必要なことは検討がついているのですが、
どのように記述すればよいかというところで手詰まりな状態です。
保存処理の方針に対しての過不足、上記記述内容のお気づきの点等ありましたら、
改めてご教示いただけると幸いです。
あなたの回答
tips
プレビュー