So I made this FastDL (sv_downloadurl) script that gets a file from my server(s) if it's not on the webhost, compresses it and then gives it to the client.
But it seems to have various bugs, for example, sometimes it will create corrupted files. I suspect it could be a conflict caused by multiple clients trying to process the same file, but not sure. Here's the most recent error messages, had a lot of others but lost them:
[CODE][27-Oct-2010 14:05:29] PHP Warning: unlink(Gmod/maps/dm_peachs_castle.bsp) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/cybergmo/public_html/FastDL/process.php on line 223
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 230
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 231
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 232
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 233
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 234
[27-Oct-2010 14:05:29] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/cybergmo/public_html/FastDL/process.php:223) in /home/cybergmo/public_html/FastDL/process.php on line 235[/CODE]
Also here's the actual script. I snipped some lines but added blank lines instead to keep line number the same (for error check):
[PHP]<?php
//CONFIG
$Min_Compress_Size = 256*1024; //Minimum size for a file to be bzipped, in bytes.
$Exclude_From_Compression = array("dua"); //a list of file types that shouldn't be bzipped.
$Incomplete_Files_Extension = "incomplete"; //what extension to add to files while they are transferred/compressed.
$Incomplete_Files_Expire = 10; //Time in seconds before incomplete files may be deleted if they haven't been modified.
/*
Servers list
Format (stuff between < > means replaced with your own info, remove < > after):
$Servers = array(
"<Server 1 Name>" => array(
"IP" => "<Server 1 IP>",
"User" => "<Server 1 FTP Username>,
"Password" => "<Server 1 FTP Password>",
"Game" => "<The game Server 1 is running>",
"Game Folder" => "<Path to Server 1's Game folder>"
),
"<Server 2 Name>" => array(
"IP" => "<Server 2 IP>",
"User" => "<Server 2 FTP Username>,
"Password" => "<Server 2 FTP Password>",
"Game" => "<The game Server 2 is running>",
"Game Folder" => "<Path to Server 2's Game folder>"
)
);
*/
$Servers = array(
//snipped
);
//END OF CONFIG
//Don't edit beyond this point unless you know what you're doing.
$server = $_GET["server"];
$relative_path = $_GET["file"];
if (!$Servers[$server]) {
exit("Invalid server specified: $server\n");
}
preg_match("/^(\/.*)?\/([^\/]+?)\.([^\.]+)??(\.bz2)?$/", $relative_path, $matches);
$relative_dir = $matches[1];
$filename_without_extension = $matches[2];
$extension = $matches[3];
$bz2_extension = $matches[4];
if (!$filename_without_extension) {
exit("Invalid file requested: $relative_path\n");
}
$uncompressed_filename = "$filename_without_extension.$extension";
$filename = $uncompressed_filename.$bz2_extension;
$uncompressed_relative_path = "$relative_dir/$uncompressed_filename";
$local_dir = $Servers[$server]["Game"].$relative_dir;
$uncompressed_path = "$local_dir/$uncompressed_filename";
$requested_path = "$local_dir/$filename";
$incomplete_uncompressed_path = "$uncompressed_path.$Incomplete_Files_Extension";
$incomplete_requested_path = "$requested_path.$Incomplete_Files_Extension";
if (!is_dir($local_dir)) {
mkdir($local_dir, 0777, true);
}
if (!is_file($requested_path)) {
while (!is_file($uncompressed_path)) {
clearstatcache();
if ( is_file($incomplete_uncompressed_path) && time() - filemtime($incomplete_uncompressed_path) >= $Incomplete_Files_Expire ) {
unlink($incomplete_uncompressed_path);
}
if (!is_file($incomplete_uncompressed_path)) {
$incomplete_uncompressed_file = fopen($incomplete_uncompressed_path, "wb");
$connection = ftp_connect($Servers[$server]["IP"], $Servers[$server]['Port']);
$login_result = ftp_login($connection, $Servers[$server]["User"], $Servers[$server]["Password"]);
if ($connection && $login_result) {
$server_dir = $Servers[$server]["Game Folder"];
$downloaded = false;
$found = false;
if (ftp_size($connection, $server_dir.$uncompressed_relative_path) != -1) {
$downloaded = ftp_fget($connection, $incomplete_uncompressed_file, $server_dir.$uncompressed_relative_path, FTP_BINARY);
$found = true;
}
if (!$found) {
$addons = ftp_nlist($connection, "$server_dir/addons");
if ($addons != false) {
foreach ($addons as $addon) {
if (ftp_size($connection, "$addon$uncompressed_relative_path") != -1) {
$downloaded = ftp_fget($connection, $incomplete_uncompressed_file, "$addon$uncompressed_relative_path", FTP_BINARY);
$found = true;
break;
}
}
}
}
if (!$found) {
$gamemodes = ftp_nlist($connection, "$server_dir/gamemodes");
if ($gamemodes != false) {
foreach ($gamemodes as $gamemode) {
if (ftp_size($connection, "$gamemode/content$uncompressed_relative_path") != -1) {
$downloaded = ftp_fget($connection, $incomplete_uncompressed_file, "$gamemode/content$uncompressed_relative_path", FTP_BINARY);
break;
}
}
}
}
ftp_close($connection);
}
fclose($incomplete_uncompressed_file);
if ($downloaded && is_file($incomplete_uncompressed_path)) {
rename($incomplete_uncompressed_path, $uncompressed_path);
} elseif (is_file($incomplete_uncompressed_path)) {
unlink($incomplete_uncompressed_path);
}
break;
}
}
}
if ($bz2_extension && is_file($uncompressed_path) && (!in_array($extension, $Exclude_From_Compression)) && filesize($uncompressed_path) >= $Min_Compress_Size) {
while (!is_file($requested_path)) {
clearstatcache();
if (is_file($incomplete_requested_path) && time() - filemtime($incomplete_requested_path) >= $Incomplete_Files_Expire) {
unlink($incomplete_requested_path);
}
if (is_file($uncompressed_path)) {
if (!is_file($incomplete_requested_path)) {
$uncompressed_file = fopen($uncompressed_path, "rb");
$incomplete_requested_file = bzopen($incomplete_requested_path, "w");
while (!feof($uncompressed_file)) {
$buffer = fgets($uncompressed_file, 4096);
bzwrite($incomplete_requested_file, $buffer, 4096);
}
fclose($uncompressed_file);
bzclose($incomplete_requested_file);
if (is_file($incomplete_requested_path)) {
rename($incomplete_requested_path, $requested_path);
}
}
unlink($uncompressed_path);
break;
}
}
}
if (is_file($requested_path)) {
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: ".filesize($requested_path));
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
flush();
$requested_file = fopen($requested_path, "r");
while (!feof($requested_file))
{
echo fread($requested_file, 65536);
flush();
}
fclose($requested_file);
}
?>[/PHP]
So, could you guys help me fix it, please?
flock
[QUOTE=Siemens;25717314]flock[/QUOTE]
Thanks! Hope this will solve my issues.
Sorry, you need to Log In to post a reply to this thread.