Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to have a web server where I can download files, whose size is determined by their filename. Something like:

Browse to http://mydomain.com/100MB -> receive any 100MB file
Browse to http://mydomain.com/200MB -> receive any 200MB file
Browse to http://mydomain.com/254MB -> receive any 254MB file
Browse to http://mydomain.com/10kb -> receive any 10kb file
Browse to http://mydomain.com/1GB -> receive any 1GB file
Browse to http://mydomain.com/3GB -> receive any 3GB file

The exact way to specify file size in the URL is not important, and neither is the data content of the file received (random data, all zero's, doesn't matter), as long as it is has the right size.

I have an apache server installed, but obviously generating every file is not possible (I'd run out of disk space fast).

Is there a way to achieve this behavior? Using an apache module or perhaps PHP, or another way?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Use mod_rewrite to capture the argument:

RewriteEngine on
RewriteRule ^/?(.*)$ generate.php?size=$1 [L]

Then just have generate.php parse $_GET['size'] and return the appropriate number of bytes.

share|improve this answer
    
I don't even need the rewrite rule if I can get generate.php to work! But wouldn't that store the entire file in memory? In the case I want to serve 100 clients a 1GB file simultaneously, that would be a problem. Perhaps I'm mistaken about this, I don't know much PHP. An example would be much appreciated... –  Coder Nr 23 Sep 10 '10 at 13:58
    
Flush every 16k or so. This will force the data down the pipe. –  Ignacio Vazquez-Abrams Sep 10 '10 at 14:15

Here is how I ended up doing what I wanted.

It seems this was very easy in PHP. After learning PHP, this is the generate.php script I created:

<?php

$size = $_GET['size'];
$partsize = 1024 * 16;
$part = str_repeat(' ',$partsize);
$partcount = intval(floor($size / $partsize));
$leftover = $size - ($partcount * $partsize);

for ($i = 0; $i < $partcount; $i++) {
    echo($part);
    flush();
}

if ($leftover > 0) {
    echo(str_repeat(' ',$leftover));
}

?>

Browsing to http://mydomain.com/generate.php?size=1000 then returns a 1000 byte file containing only spaces.

The accepted answer, which set me on the track of PHP, suggests using a rewrite rule, but I don't need it for what I'm doing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.