[SOLVED] random filename and fileread()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

[SOLVED] random filename and fileread()

Post by nirus »

After a user hits submit it goes through the following code making a file with a random name that ends with .bat
When sending the file to the user the user gets Resource ID#2.
When I change the filename to be a non random name... it downloads just fine.
I am 99% certian that it has something to do with the random filename.
anyone know a solution for me?

Code: Select all

//Many people may use this at one time so make random file names
//so files are not overwriten and or errors are not made
srand ((double) microtime( )*1000000);
$ranum1 = rand(0,10);
srand ((double) microtime( )*1000000);
$ranum2 = rand(0,10);
$ind = mt_rand(0, 25);
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$letter = $alphabetї$ind];
$filename = "serverbat$ranum1$letter$ranum2.bat";

//Create the bat file and open it for writing to...
$serverbat = fopen("$filename", "x+");

//write my contents
fwrite($serverbat, $content1); 
fwrite($serverbat, $content2);

//close the bad boy up
fclose($serverbat);

//send the file
$size = filesize($serverbat);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$serverbat");
header("Content-Length: $size");
I have also tried and failed with:
header("Content-Disposition: attachment; filename=\"$serverbat\"");

- Thanks for who ever helps me out on this one
Nirus
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use $filename, not $serverbat. $serverbat is a system resource, a file handle to the actual file, not the filename or anything like that.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

$serverbat is a filepointer to a file (more general a resource)

Code: Select all

$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$ind = mt_rand(0, strlen($alphabet) - 1);
$letter = $alphabet{$ind};
$filename = "serverbat{$ranum1}{$letter}{$ranum2}.bat"; 

...

//send the file
$size = filesize($filename);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size")
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

1st...Thanks for the help!
I am using php expert editor which did not find serverbat as reserved, but I changed it b/c your probably right :P.
Im still getting resource ID#2 as shown in my error below

Code: Select all

srand ((double) microtime( )*1000000);
$ranum1 = rand(0,10);

srand ((double) microtime( )*1000000);
$ranum2 = rand(0,10);

$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$ind = mt_rand(0, strlen($alphabet) - 1);
$letter = $alphabet{$ind};
$filename = "myfile{$ranum1}{$letter}{$ranum2}.bat";

//Create the bat file and open it for writing to...
$filename = fopen("$filename", "x+");

      //enter content into file here

//just to see where I stand at this point
echo $filename;
echo "<br>after close: ";
fclose($filenme);
echo $filename;
echo "<br>";


$size = filesize($filename);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size");

Here is my error/output:
Resource id #2
after close: Resource id #2

Warning: filesize(): Stat failed for Resource id #2 (errno=2 - No such file or directory) in E:\Apache\Apache2\htdocs\nirus\phpserverbat\phpserverbat.php on line 336
Warning: Cannot modify header information - headers already sent by (output started at E:\Apache\Apache2\htdocs\nirus\phpserverbat\phpserverbat.php:328) in E:\Apache\Apache2\htdocs\nirus\phpserverbat\phpserverbat.php on line 337

If you notice the "no such file or directory"... that why I think the whole random number thing is throwin it off.
-Nirus
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

Ahhhh you ment filename from this line!!
I'll try that :P
$whatever = fopen("$filename", "x+");

-Nirus
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

Thanks a lot man... that worked!

-Nirus
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

Everything is working nice and good but, this is strange...
The file it creates does contain the writen material
the file that is "downloaded" or sent with headers has no code in once you download it
The downloaded file and the file created on the server have the same name even though I have my random number thing
Not sure why the download has no code in it.

Code: Select all

srand ((double) microtime( )*1000000);
$ranum1 = rand(0,9);
$alphabet = 'abcdef';
$ind = mt_rand(0, strlen($alphabet) - 1);
$letter = $alphabet&#123;$ind&#125;;
$filename = "myfile&#123;$ranum1&#125;&#123;$letter&#125;.bat";

//Create the bat file and open it for writing to...
$mybat = fopen("$filename", "x+");
//Get contents for all bat files and write it
$allbat = file_get_contents('allbat.inc'). "\r\n";
fwrite($mybat, $allbat);    

     fclose($mybat);


     //Send the new bat to the user here!!!!

     $size = filesize($filename);
     header ("Content-Type: application/octet-stream");
     header("Content-Disposition: attachment; filename=$filename");
     header("Content-Length: $size");
thank you too much for any help!!
-Nirus
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's possible your version of php/OS doesn't support the "x+" mode used in fopen.. try using "wb" or "wb+"
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

Well I tried the wb and the wb+

I think it has something to do with the rndom file name. I think it might be making a whole new file in memory someplace with no data in it.. then downloading that.

I'm stuck on this one

-Nirus
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

didn't notice that you didn't actually send the file. :) Which can help ;)

Code: Select all

// header calls here

readfile($filename);
nirus
Forum Newbie
Posts: 16
Joined: Fri Dec 17, 2004 12:35 am
Location: Iowa

Post by nirus »

HOLEY COW... thats what happens when you have a 2 mile long script.
Can't believe I did not see that. thanks for catching my ignorance
- file has content in it now :)

-Nirus
Post Reply