2011年3月31日木曜日

3/31覚書

1時間目

reply_add実装続き

2時間目

reply_add実装

idを引き継ぐ処理以外は基本おなじなのだねー。

ていうかそのreplyを表示させるのががが。

3時間目
画像アップロード

[name] => notice01.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpyCW55n
[error] => 0
[size] => 83218

ファイル送信時次のプログラムの処理が終わるまでの間、
サーバーのTEMPディレクトリに一時保管されている。

sleep(20);を加えると20秒間処理を止めるので、
その間にPuTTYからrootで入ってcd /tmpで中身を見ると、
-rw------- 1 apache apache 83218 3月 25 16:25 phpJNeLpc
ってファイルがsleepで設定した時間分だけ確認できる。
処理が終われば消える。

4時間目

画像アップロード処理続き
TEMPディレクトリ作成とパーミッション変更

cd web/uploads
mkdir tmp
chmod -R 777 tmp


画像のアップロード
実際に表示させてみる


5時間目
photoカラム追加
ALTER TABLE comment ADD photo text;
schema.yml、model作成
/web/images/commentディレクトリ作成
chmod 777 commentでパーミッション作成

あと画像表示
できたらpreplyも。
replyカラム+/web/images/replyカラムつけてね。

6時間目

あと画像表示
できたらpreplyも
作業続き

めんどいからここでコード。
action.class.php
<?php

class reply_addActions extends sfActions
{
  public function executeConfirm()
  {
   $this->id = $this->getRequestParameter("id");
 $this->name = $this->getRequestParameter("name");
 $this->coment = $this->getRequestParameter("coment");
 $photo = $this->getRequest()->getFile('photo');

   if($photo["tmp_name"] != '') {

  //保存する画像名はセッションID生成し付与
  session_regenerate_id();
  //myUtil::getFileExtで拡張子判別。帰り値は._gif._png._jpg
  $tmpname = 'photo_' . session_id() .myUtil::getFileExt( $photo["tmp_name"] );
  //web/uploads/tmpへ画像を移動する
  $this->getRequest()->moveFile('photo',sfConfig::get('sf_upload_dir').'/tmp/'.$tmpname);

  //templateにパスを渡している
  $this->photo = $tmpname;
 }

 return sfView::SUCCESS;
  }

  public function executeInput()
  {
   //GETで渡されてくるコメントID
   $id = $this->getRequestParameter("id");
   $this->comment = CommentPeer::retrieveByPK($id);
   $this->photo = $this->getRequestParameter("photo");


 return sfView::SUCCESS;
  }

  public function executeResult()
  {
 //idとnameとmailを変数にして渡す。
 $id = $this->getRequestParameter("id");
 $name = $this->getRequestParameter("name");
 $coment = $this->getRequestParameter("coment");

 //replyのupdate
 //インスタンス化。空のオブジェクトを生成。
 $reply = new Reply();
 //変数にした名前とメールを入力。
 $reply->setCommentid("$id");
 $reply->setNickname("$name");
 $reply->setContent("$coment");
 //insert文発行
 $reply->save();

 $photo = $this->getRequestParameter('photo');

  //画像の存在判定
  if($photo != '') {

   ///uploads/tmp/'.$photoから画像を取得
   $src = sfConfig::get('sf_web_dir').'/uploads/tmp/'.$photo;
   //最新(投稿と同一)のidを取得(autoincrement稼動時)
   $saveName = $reply->getId().myUtil::getFileExt($src);
   //TEMPから/images/comment/".$saveNameへ保存
   $dst = sfConfig::get('sf_web_dir')."/images/reply/".$saveName;

   rename($src, $dst);
   $reply->setPhoto($saveName);
   $reply->save();

  }

   return sfView::SUCCESS;
  }
}

inputSuccess.php
このコメントに返信します
<div>
<?php echo $comment->getContent();?>
  <?php if($comment->getPhoto()):?>
  <img src="/images/comment/<?php echo $comment->getPhoto();?>"><br><br>
  <?php endif;?>
</div>

<div class="line">
</div>
返信フォーム<br>

<form action="/reply_add/confirm" method="post" enctype="multipart/form-data">
<textarea name="coment"></textarea><br>
ニックネーム<input type="text" name="name" size="20">
<br>
<input type="file" name="photo"><br>
<input type="hidden" name="id" value="<?php echo $comment->getId();?>">
<input type="submit" value="確認する">
</form>
confirmSuccess.php
<form action="/reply_add/result" method="post">
返信内容<br>
<?php echo $coment;?>
<input type="hidden" name="coment" value="<?php echo $coment;?>">
<br>
ニックネーム<br>
<?php echo $name;?>
<br>
画像<br>
<?php if($photo !=""):?>
<img src="/uploads/tmp/<?php echo $photo?>">
<br><br>
<div class="line"></div>

<input type="hidden" name="photo" value="<?php echo $photo;?>">
<?php endif;?>
<input type="hidden" name="name" value="<?php echo $name;?>">
<input type="hidden" name="id" value="<?php echo $id;?>">
<br>

<input type="submit" value="確認する">
</form>

resultSuccess.php
完了しました。
<a href="/">トップに戻る</a>

パーミッション解説
自分とグループと他人に対してのアクセス権限。
ファイルへの読み(read) 書き(write) 実行(execute)についての権限を与えること。

できる人だけ。
アップロード3つぐらい送って見る。
複数の条件がどーしても思い浮かばない・・・
$photoをどこで複数化するんだああああ。

おまけ
ntpやってなかったので設定。
教科書のサーバー構築の一番下にあるntp設定やればいいよ。
あとcreated_atの表示で詰まった。
_含むばあいは_取っていいみたい。
例】
投稿日時getCreatedat();?>