2009-05-27 9 views
1

最近私はプロジェクトの名前を変更する必要があるプロジェクトで働いていました。 [function.rename](シーカー/ SeekerPhoto/katr.jpg、シーカー/ SeekerPhoto/ussl.jpg)の名前を変更:いいえ、私はそれが名前を変更した画像の名前を変更したが怒鳴る"No such file or directory"という警告メッセージを避けるには

警告のような警告メッセージを表示するときに問題がありますそのようなファイルやディレクトリは/subdomains/www/html/ussl/job/insphoto.php on line 100

この警告メッセージを回避するにはどうすればよいですか。それは、警告がどんなものであったとしても、次の作業に向かうことを意味します。

答えて

0

あなたは単なる警告を抑制することを意味している場合、あなたは、単一のステートメントでエラーメッセージを抑制し@ operatorを、使用することができます@演算子

$file = @operation(); 
6

を使用することができます。あなたが複数の文のための警告を抑制したい場合

@rename($oldFileName, $newFileName); 

代わりに、あなたはerror_reporting値を減らすことができる:

$oldErrorReportingValue = error_reporting(0); 
rename($oldFileName, $newFileName); 
# do something else .... 
error_reporting($oldErrorReportingValue); 

注意警告が理由であること。最良の方法は、を調べて、なぜオペレーションが警告を生成し、コードがこれらの状況を処理できるかどうかを確認することです。最後の手段として警告を無視する必要があります。

+0

です問題を隠しているだけです... –

+0

それは部分的にしか正しいのではなく、それを反映するように答えを更新しました。時にはそのようなエラー処理が本当に必要です - VolkerKの応答を見て、競合状態の場合は警告を表示します。 – soulmerge

0

あなたがあなたのphpファイルの先頭にこれを追加する必要がありますini_set方法のためのPHPマニュアルを見てだけでなく、appendix ことができます。

ini_set('display_errors','1'); 
12

あなたは上のいくつかのテストを行うことができますパラメータを変更してからファイル名を変更してください。

if (!file_exists($oldfile) || !is_readable($oldfile)) { 
    // some error handling here 
} 
else { 
    $b = rename($oldfile, $newfile); 
}

編集:私はこの回答が他のものと比較してそれほど大きくないことを期待していませんでした。コメントにご注意ください。ここでは事前に警告メッセージを表示する可能性のあるすべての条件をテストすることは非常に困難です。実際のアクションを実行すると失敗する可能性があるファイルシステム上で何をテストしていますか?テストテーブルのフィールド、権限、またはこれまでに必要なものは依然としてSQLクエリが失敗する可能性があります(たとえば、2006年のMySQLサーバーがなくなり、いつでも実行できます)。それではそれにもかかわらず、より可能性の高い原因についてパラメータをテストし、スクリプトがこれらのエラーを「正常に」処理できるようにします。一緒に使用

+0

+1これは正しい方法です – soulmerge

+0

YAY! PHP /と/を知っている人は、実際にクリーンなコードを書く方法を知っています。 –

+0

...それでも完全に正しいわけではありません。古いファイルへの読み取り/書き込みアクセスをチェックし、新しいファイルのアクセス許可を書き込む必要があります。しかし、とにかく...それはほんの一例です;-)そして、私はこのケースで@に完全に反対していません。あなたは警告の出力を抑制し、戻り値をチェックしたり、独自のエラーハンドラを設定したり、track_errors/$ php_errormsgを使用することができます。 – VolkerK

3

2つのことは、最高のサービスを提供する必要があります

  1. error_reporting()
  2. ini_set('display_errors', (boolean)showInBrowser)

使用error_reporting()警告メッセージの詳細の適切なレベルを設定します。警告、通知、および/またはエラーが記録されるかどうかを設定するだけで、表示されるかどうかは設定されません。

おそらく "error_reporting(E_ERROR | E_USER_ERROR);"本当に何かを壊していない通知や警告だけでなく、実際にはエラーであれば何かを記録します。

このような何かを行うにはおそらく良い考えであるすべてのすべて:

if (getenv('PHP_DEBUG')=='1') 
{ 
    error_reporting(E_ERROR | E_USER_ERROR); 
    ini_set('display_errors', true); 
} 
else 
{ 
    error_reporting(E_ERROR | E_USER_ERROR); 
    ini_set('display_errors', false); 
} 

をそして開発サーバー上で、あなたの.htaccessやVirtualHostディレクティブに以下の行を持つことができます:

SetEnv PHP_DEBUG=1 

サイドノートでは≠1

を設定していないので、本番では全くそれを設定する必要はありません、私は個人的に私のerror_reportingを持っていることを好むのように設定しますあなたは、私はちょうどいくつかをチェックすることによって、すべての通知と警告を倒すことができればと感じているので、「そう、より良い仕事をするために私を強制的に、私はおそらく間違って行っていることができるすべてに警告する」と英語で読むことができる

error_reporting(E_ALL | E_STRICT); 
変数を適切に初期化すると、最終結果はおそらく少なくとも少し安全になります。

編集:いくつかの明確化:

アリフだけでメッセージを取得しないために、操作が成功したことを確認するために要求していないので。私は「操作がうまくいくかどうかは気にしない」と解釈した。それについて行くのより良い方法は、あなたの関数ライブラリに次のようなものになるだろう勿論:

/** 
* @author: Kris 
* @license: see http://sam.zoy.org/wtfpl/ 
* 
* PLEASE NOTE THAT THE FOLLOWING CODE IS TESTED BY ME, NOT QUALITY ASSURANCE 
*/ 

/** 
* Move a file 
* 
* If uses filename from $source if $destination is a directory 
* 
* @param string $source 
* @param string $destination 
* @param bool $overwrite 
* @return bool 
*/ 
function my_move_file($source, $destination, $overwrite = false) 
{ 
    return _internal_my_move_or_copy_file($source, $destination, true, $overwrite); 
} 

/** 
* Copy a file 
* 
* If uses filename from $source if $destination is a directory 
* 
* @param string $source 
* @param string $destination 
* @param bool $overwrite 
* @return bool 
*/ 
function my_copy_file($source, $destination, $overwrite = false) 
{ 
    return _internal_my_move_or_copy_file($source, $destination, false, $overwrite); 
} 

define('__internal_my_move_or_copy_file_e_error', E_USER_ERROR);  // change to E_USER_NOTICE if not meant to be fatal 
define('__internal_my_move_or_copy_file_e_notice', E_USER_NOTICE); 

/** 
* Should not be called by userland code, use my_move_file or my_copy_file instead 
* 
* one function to implement both move and copy because almost all of the required validations is identical. 
* 
* @param string $source 
* @param string $destination 
* @param bool $is_move 
* @param bool $overwrite 
* @return bool 
*/ 
function _internal_my_move_or_copy_file($source, $destination, $is_move, $overwrite) 
{ 
// what we'll be returning 
    $result = false; 

    // input sanity checks 
    if (!is_string($source) || !is_callable($source, '__toString')) 
    { 
     trigger_error( 
      "_internal_my_move_or_copy_file: expects \$source to be a string.", 
      __internal_my_move_or_copy_file_e_error); 
     return false; 
    } 
    elseif (!is_string($destination) || !is_callable($destination, '__toString')) 
    { 
     trigger_error( 
      "_internal_my_move_or_copy_file: expects \$destination to be a string.", 
      __internal_my_move_or_copy_file_e_error); 
     return false; 
    } 
    elseif (! is_bool($is_move)) 
    { 
     trigger_error( 
      "_internal_my_move_or_copy_file: expects \$is_move to be a bool.", 
      __internal_my_move_or_copy_file_e_error); 
     return false; 
    } 
    elseif (! is_bool($overwrite)) 
    { 
     trigger_error( 
      "_internal_my_move_or_copy_file: expects \$overwrite to be a bool.", 
      __internal_my_move_or_copy_file_e_error); 
     return false; 
    } 

    $action_word = $is_move ? 'move' : 'copy'; 

    if (file_exists($source) && is_readable($source)) 
    { 
     $to = preg_split('/\//', $destination, -1, PREG_SPLIT_NO_EMPTY); 
     $destination = '/'.implode('/', $to); 

     if (is_dir($destination)) 
     { 
     // make sure we don't accidentally allow ../ etc 
      if (in_array('..', $to) || in_array('.', $to)) 
      { 
       trigger_error("my_{$action_word}_file: \$destination does not allow path traversion using /../ or /./", $e_error_code); 
      } 

      // make sure we have a filename on $destination 
      if (is_dir($destination)) 
      { 
      // user gave a directory but no filename so use the filename in $source 
       $to[] = basename($source); 
       $destination = '/'.implode('/', $to); 
      } 
     } 

     if (file_exists($destination) && is_writable($destination)) 
     { 
      if (! $overwrite) 
      { 
       trigger_error(
        "my_{$action_word}_file: \$destination already exists and I am instructed not to overwrite.", 
        __internal_my_move_or_copy_file_e_notice); 
       return false; 
      } 
     } 
     elseif (is_dir(dirname($destination)) || is_writable(dirname($destination))) 
     { 
     // we can write 
     } 
     else // all allowable situations are already passed 
     { 
      trigger_error( 
       "my_{$action_word}_file: $destination directory does not exist or cannot be written to.", 
       __internal_my_move_or_copy_file_e_error); 
     } 


     if ($is_move) 
     { 
     // if we are going to move a file the source also needs to be writable 
      if (! is_writable($source)) 
      { 
       trigger_error( 
        "my_{$action_word}_file: Cannot {$action_word} \$source because it cannot be written.", 
        __internal_my_move_or_copy_file_e_error); 
      } 

      $result = rename($source, $destination); 
     } 
     else 
     { 
      $result = copy($source, $destination); 
     } 

     // see if what php's built in function gave us is acceptible 
     if ($result === false) 
     { 
      trigger_error( 
       "my_{$action_word}_file: unexpected failure to {$action_word} \$source to \$destination.", 
       __internal_my_move_or_copy_file_e_error); 
     } 

     // postflight check if the work we did was successful 
     if (!file_exists($destination)) 
     { 
      trigger_error( 
       "my_{$action_word}_file: unexpected failure to {$action_word} \$destination does not exist after {$action_word} operation.", 
       __internal_my_move_or_copy_file_e_error); 
     } 
    } 
    else // file does not exists or is unreadable 
    { 
     trigger_error( 
      "my_{$action_word}_file: \$source \"$source\" does not exist or cannot be read.", 
      __internal_my_move_or_copy_file_e_error); 
    } 

    return $result; 
} 
+0

何かがコード内のインデントを台無しにして、私はそれが起こったときに私はそれが嫌い: - / – Kris

0

あなたは「dispaly_errors」を使用することができます

APIは、あなたがしているhere

関連する問題