0

このコードを使用すると、3 つのエラーが発生し続けます。

Warning: fopen() [function.fopen]: Filename cannot be empty
Warning: fwrite(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource

どうすればいいのかわからない。私はphp初心者です。

<?php

$random = rand(1, 9999999999);
$location = "saves/".$random;


while (file_exists($location)) {
$random = rand(1, 999999999999);
$location = "saves/".$random;
}

$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
4

3 に答える 3

1

編集前の元の質問によると:

ファイルがまだ存在しないため、while条件が機能せず、エラー メッセージが表示されるのはそのためです。

また、ファイルに乱数を使用しているため、そもそもどのファイルを開くべきかわかりません。whileループを外すだけ。

これを試して:

<?php
$random = rand(1, 999999999999);
$location = "saves/".$random;

$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
于 2013-12-29T03:08:22.303 に答える
0

あなたが持っているコードから、 $location は while ループのスコープ内にのみ存在するようです。試す

<?php

$location = "";
while (file_exists($location)) {
    $random = rand(1, 999999999999);
    $location = "saves/".$random;
}

$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
于 2013-12-29T03:10:23.433 に答える
0

まず、$location変数に値を設定するか、ファイルがまだ作成されていないため、これを試してください:

$random = rand(1, 999999999999);
$location = "saves/".$random;


$content = "some text here";

//if(file_exists($location)) $fp = fopen($location,"wb");
$fp = fopen($location, 'wb') or die('Cannot open file:  '.$location); //implicitly creates file

fwrite($fp,$content);
fclose($fp);
于 2013-12-29T03:21:27.757 に答える