0

I keep getting 3 errors when I use this code:

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

I don't know what to do. I'm a php noob.

<?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);
?>
nulldev
  • 97
  • 1
  • 3
  • 10
  • May I ask how to? Sorry, first time. – nulldev Dec 29 '13 at 03:04
  • Shouldn't you also get a Notice then about `$location` being empty? – mario Dec 29 '13 at 03:04
  • I did but It isn't. Well not after the loop. – nulldev Dec 29 '13 at 03:06
  • 1
    Since the file doesn't exist yet, your `while` condition won't work and that's why you're getting those error messages. And since you're using a random number for the file, you will never know which file to open in the first place. Just remove the `while` loop. – Funk Forty Niner Dec 29 '13 at 03:06
  • Ok I changed it but still getting same errors except for the first one. Its now: Warning: fopen(saves/1354473988) [function.fopen]: failed to open stream: No such file or directory in – nulldev Dec 29 '13 at 03:07
  • I noticed your edit. You can't do that neither. It won't find the file because the random number keeps changing. You need to save the random variable as the file name. – Funk Forty Niner Dec 29 '13 at 03:10
  • `fopen()` should attempt to create the file if you're passing it the `w` mode argument, but I think it might fail if the directory doesn't exist. (See [here](http://stackoverflow.com/questions/10877007/php-fopen-no-such-file-or-directory).) Could that be the problem? – Ben Dec 29 '13 at 03:12
  • Using a `do {} while ()` would accomplish the intended task. – mario Dec 29 '13 at 03:13
  • What is it that you're trying to achieve here, append to the random file that was created? If your folder doesn't have write permissions, check that too. – Funk Forty Niner Dec 29 '13 at 03:14

3 Answers3

1

As per your original question before your edit:

Since the file doesn't exist yet, your while condition won't work and that's why you're getting those error messages.

And since you're using a random number for the file, you will never know which file to open in the first place. Just remove the while loop.

Try this:

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

$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • That would work but not in my condition. I'm trying to make a program that will save to a random URL than give that URL to the user. The user can then access the file whenever they want. I don't want the users saving over other people's saves. Ignore the security loopholes for now. – nulldev Dec 29 '13 at 03:10
  • You don't need a `WHILE` condition then. Are you trying to check if a file of the same name already exists before creating a new random file? @user2898813 If so, then you'll need to create a function that will save the filename to a log file at the same time the new file is created, then check in there first to see if it exists. I would've guided you better if you would have posted what you just told me, in your question, instead of just error codes. – Funk Forty Niner Dec 29 '13 at 03:23
  • You're welcome. If this has answered your question, let's close the question properly then, by clicking the white checkmark next to my answer till it turns green. Otherwise, it will remain in the unanswered category. @user2898813 – Funk Forty Niner Dec 29 '13 at 03:31
  • TIP: You can use `mt_rand` instead of `rand` to get a better random number `$random = mt_rand(1, 9999999999);` and it's a faster function. @user2898813 – Funk Forty Niner Dec 29 '13 at 03:34
0

From the code you have, it looks like $location only exists inside the scope of the while loop. Try

<?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);
?>
anyaelise
  • 92
  • 1
  • 7
0

first of all, you must set values to your $location variable or since the file is not yet created try this:

$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);