Eléphanteau du PHP |
21 Messages
15 oct. 2012, 13:17
j'ai réussi en faisant cela :
<?php
$token = $_GET['q'];
$unid = $_GET['f'];
if( strlen($token)<32 )
{
die("Invalid token!");
}
include ('connectdb.php');
$bdd=connectdb();
$filedir=$bdd->query('SELECT file_dir,file_name FROM files WHERE unic_id="'.$unid.'"');
$filedir2=$filedir->fetch();
$filename=$filedir2['file_name'];
$filedir=$filedir2['file_dir'];
$filedir=str_replace("xxx","",$filedir);
/* Define the secret file: */
$secretfile = $filedir;
/* This variable is used to determine if the token is valid or not: */
$valid = 0;
/* Define what file holds the ids. */
$file = "tmp/urls.txt";
/* Read the whole token-file into the variable $lines: */
$lines = file($file);
/* Truncate the token-file, and open it for writing: */
if( !($fd = fopen("tmp/urls.txt","w")) )
die("Could not open $file for writing!");
/* Aquire exclusive lock on $file. */
if( !(flock($fd,LOCK_EX)) )
die("Could not equire exclusive lock on $file!");
/* Loop through all tokens in the token-file: */
for( $i=0; $lines[$i]; $i++ )
{
/* Is the current token the same as the one defined in $token? */
if( $token == rtrim($lines[$i]) )
{
$valid = 1;
}
/* The code below will only get executed if $token does NOT match the
current token in the token file. The result of this will be that
a valid token will not be written to the token file, and will
therefore only be valid once. */
else
{
fwrite($fd,$lines[$i]);
}
}
/* We're done writing to $file, so it's safe release the lock. */
if( !(flock($fd,LOCK_UN)) )
die("Could not release lock on $file!");
/* Save and close the token file: */
if( !(fclose($fd)) )
die("Could not close file pointer for $file!");
/* If there was a valid token in $token, output the secret file: */
if( $valid )
{
switch(strrchr(basename($Fichier_a_telecharger), ".")) {
case ".gz": $type = "application/x-gzip"; break;
case ".tgz": $type = "application/x-gzip"; break;
case ".zip": $type = "application/zip"; break;
case ".pdf": $type = "application/pdf"; break;
case ".png": $type = "image/png"; break;
case ".gif": $type = "image/gif"; break;
case ".jpg": $type = "image/jpeg"; break;
case ".txt": $type = "text/plain"; break;
case ".htm": $type = "text/html"; break;
case ".html": $type = "text/html"; break;
case ".avi": $type = "video/avi"; break;
default: $type = "application/octet-stream"; break;
}
header("Content-disposition: attachment; filename=".$filename);
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: $type\n"); // Surtout ne pas enlever le \n
header("Content-Length: ".filesize($filedir));
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");
readfile($filedir);
}
else
{
Header('Location:index.php');
}
?>
Mais ne fonctionne pas pour les fichiers gros!