Mám na mojej stránka upload cez php (formou include) na server a download zo serveru (cez include) ! Upload funhuje bez problémov - lebo keď do adresára pozriem cez FTP tak sú tam súbory čo som upol a keď si ich nahrém vo Wine na plochu idú v pohodke otvoriť !!!
Problém mám však s downloadom - v súbore downloader.php, ktorý na stránku volám formou include sa deje niečo také, že pri stiahnutí súborov, ktoré sú na servery v poriadku, sa do nich neviem prečo na ich začiatok vloží celý zdrojový kód stránky od <html> po </html> a až potom sú ostatné údaje súboru (tie čo tam majú byť) ! Teda mi nič čo je stiahnuté so serveru nejde ! Napr. obrázok, keď naň kliknem mi neotvorí a dokonca pri sťahovaní si opera myslí že je to *.html súbor a nie obrázok.
Poraďte prosím čo tam mám upraviť - podľa mňa je to niečo s tým urlencode - ale neviem !!!
Pr. : tu je php súbor, ktorý by my mal normálne sťahovať ?
Kód:
<?php
$standalone = "true";
$custom_script_name = "downloader.php";
$enable_stats = "false";
$download_stats_file = "statistika";
$download_folder = "nas/";
$auto_create_filelist = "true";
// define download filelist when auto_create_filelist is set to "false"------------------------------------------------------------
$download_filelist = array( "name1" => array( "file1.zip",
"application/octet-stream",
"description1"),
"name2" => array( "subfolder1/file2.zip",
"application/octet-stream",
"description2"),
"name3" => array( "subfolder2/file1.zip",
"application/octet-stream",
"description3"));
// standalone modus configuration-----------------------------------------------------------------------------------------------------------
$headline_string = "Zoznam súborov dostupných na stiahnutie !";
$css_code = <<<CSS_CODE
CSS_CODE;
// !!! dont't change the position of the operator CSS_CODE and don't write anything behind, it must be alone at the beginning of the line
//################################################################################
//### ####
//### CONFIGURATION - ends here ####
//### ####
//### !!! don't edit anything above, except you know what you are doing !!! ####
//### ####
//################################################################################
$download = $_GET["download"];
$SCRIPT_NAME = getenv("SCRIPT_NAME");
// auto create download filelist-------------------------------------------------------------------------------------------------------------
if ($auto_create_filelist=="true") {
unset($download_filelist);
$current_dir=getcwd();
chdir($download_folder);
$directory=dir("./");
while ($file = $directory->read()) {
if (is_file($file) and $file!=basename($SCRIPT_NAME) and $file!=basename($download_stats_file) and $file!=$custom_script_name) {
$download_filelist[$file][0]=$file;
$download_filelist[$file][1]="application/octet-stream";
}
}
$directory->close();
chdir($current_dir);
}
// download handling--------------------------------------------------------------------------------------------------------------------------
if (isset($download)) {
if (isset($download_filelist[$download])) {
$filename=$download_folder.$download_filelist[$download][0];
if (file_exists($filename)) {
if ($enable_stats=="true") {
if (file_exists($download_stats_file) and is_writable($download_stats_file)) {
foreach (file($download_stats_file) as $line) {
$array=explode("|",$line);
$download_stats_file_array[$array[0]]=$array[1];
}
$download_stats_file_array[$download]++;
$handle=fopen($download_stats_file,"w");
foreach ($download_stats_file_array as $value) {
fputs($handle,key($download_stats_file_array)."|".$value."|\n");
next($download_stats_file_array);
}
fclose($handle);
}
else { die("Sorry, nie sú prístupné sťahovania."); }
}
header("Content-Type: ".$download_filelist[$download][1]);
header("Content-Disposition: attachment; filename=\"".basename($filename)."\"");
readfile($filename);
exit;
}
else { die("Sorry, ale požadovaný súbor sa nenašiel."); }
}
else { die("Sorry, ale požadované sťahovanie neexistuje."); }
}
// initialize global variables-----------------------------------------------------------------------------------------------------------------
$GLOBALS["SCRIPT_NAME"] = getenv("SCRIPT_NAME");
$GLOBALS["enable_stats"] = $enable_stats;
$GLOBALS["download_stats_file"] = $download_stats_file;
$GLOBALS["download_folder"] = $download_folder;
$GLOBALS["auto_create_filelist"] = $auto_create_filelist;
$GLOBALS["download_filelist"] = $download_filelist;
// functions------------------------------------------------------------------------------------------------------------------------------------------
function manage_download_stats()
{
$download_stats_file = $GLOBALS["download_stats_file"];
$download_filelist = $GLOBALS["download_filelist"];
// create download stats file if missing-------------------------------------------------
if (!file_exists($download_stats_file)) {
$handle=fopen($download_stats_file,"w");
foreach($download_filelist as $array) {
fputs($handle,key($download_filelist)."|0|\n");
next($download_filelist);
}
fclose($handle);
}
// read download stats file-------------------------------------------------------------------
foreach (file($download_stats_file) as $line) {
$array=explode("|",$line);
$download_stats_file_array[$array[0]]=$array[1];
}
return $download_stats_file_array;
}
function print_download_table()
{
$enable_stats = $GLOBALS["enable_stats"];
$download_folder = $GLOBALS["download_folder"];
$auto_create_filelist = $GLOBALS["auto_create_filelist"];
$download_filelist = $GLOBALS["download_filelist"];
$SCRIPT_NAME = $GLOBALS["SCRIPT_NAME"];
// get download stats----------------------------------------------------------------------------
if ($enable_stats=="true") { $download_stats_file_array=manage_download_stats(); }
// create download table from download file list------------------------------------
echo "<table color=\"white\" ><tr><th><font color=\"#00CCFF\" face=\"arial\" size=\"2\">Súbor</font></th>";
if ($auto_create_filelist!="true") { echo "<th>description</th>"; }
echo "<th><font color=\"#00CCFF\" face=\"arial\" size=\"2\">Veľkosť súboru</font></th>";
if ($enable_stats=="true") { echo "<th>Stiahnutia</th>"; }
echo "</tr>";
foreach($download_filelist as $download_filelist_array) {
if (!isset($download_stats_file_array[key($download_filelist)])) { $download_stats_file_array[key($download_filelist)]=0; };
echo "<tr><td><a href =\"$SCRIPT_NAME?download=".urlencode(key($download_filelist))."\" title=\"Klikni a sťahuj !\" ><font color=\"8fc84d\" face=\"arial\" size=\"2\"><b>".key($download_filelist)."</b></font></a></td>";
if ($auto_create_filelist!="true") { echo "<td><font color=\"white\" face=\"arial\" size=\"2\">$download_filelist_array[2]</font></td>"; }
echo "<td><font color=\"white\" face=\"arial\" size=\"2\">".sprintf("%.1f",@filesize($download_folder.$download_filelist_array[0])/1024)."K</font></td>";
if ($enable_stats=="true") { echo "<td><font color=\"#00CCFF\">".$download_stats_file_array[key($download_filelist)]."</font></td>"; }
echo "</tr>";
next($download_filelist);
}
echo "</table>";
}
// standalone output------------------------------------------------------------------------------------------------------------------------------
if ($standalone=="true") {
// header--------------------------------------------------------------------------------------------------------------------------------------------------
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" >";
echo "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\" />";
echo "<meta name=\"author\" content=\"MiStr\" />";
echo "<meta name=\"description\" content=\"download system\" />";
echo "<title>MiStr</title>";
// body-----------------------------------------------------------------------------------------------------------------------------------------------------
echo "".$headline_string."";
print_download_table();
// footer----------------------------------------------------------------------------------------------------------------------------------------------------
echo "</body></html>";
}
?>
Prišiel som na to, že keď ten skript pustím samostatný - teda mimo mojej stránky (bez include) tak ide normálne, ale keď ho spustím na stránke - už pridáva do súborov sťahovaných zo serveru ten kód mojej stránky a nejdú otvoriť - lebo je na ich začiatku primiešaný ten kód <html> kod celej stranky </html>
Zatiaľ som to dočasne poriešil cez iframe (trochu som to os..l), ale aj tak by som chcel od oborníkov radu, prečo to nejde cez include (vlastne ide ale dopĺňa mi to súbory na serveri a kód html !)