Page 1 of 1
[SOLVED] random filename and fileread()
Posted: Mon Jan 17, 2005 10:05 am
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
Posted: Mon Jan 17, 2005 10:08 am
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.
Posted: Mon Jan 17, 2005 10:12 am
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")
Posted: Mon Jan 17, 2005 10:41 am
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

.
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
Posted: Mon Jan 17, 2005 10:43 am
by nirus
Ahhhh you ment filename from this line!!
I'll try that

$whatever = fopen("$filename", "x+");
-Nirus
Posted: Mon Jan 17, 2005 10:55 am
by nirus
Thanks a lot man... that worked!
-Nirus
Posted: Mon Jan 17, 2005 6:39 pm
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{$ind};
$filename = "myfile{$ranum1}{$letter}.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
Posted: Mon Jan 17, 2005 6:52 pm
by feyd
it's possible your version of php/OS doesn't support the "x+" mode used in fopen.. try using "wb" or "wb+"
Posted: Mon Jan 17, 2005 7:10 pm
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
Posted: Mon Jan 17, 2005 8:00 pm
by feyd
didn't notice that you didn't actually send the file.

Which can help
Code: Select all
// header calls here
readfile($filename);
Posted: Mon Jan 17, 2005 8:07 pm
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