Jump to content

using excel files on localhost works ok but not on remote server


steve012345

Recommended Posts

I have got this code which allows me to download an excel file containing a dump of my studentanswerunit table which works fine when running on my localhost server. When using this code on my remote server however i get the error message "can't open file".

 

<?php session_start();

error_reporting(0);
if(isset($_SESSION['user'])==""){
require('userlogin.php');
exit();
}

include "config.php";
$myFile = "test.xls";

$querystring = "SELECT * FROM `studentanswerunit`";
$mysqlquery = mysql_query($querystring);

$myData = "<table width=\"100%\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" border=\"1\">
<tr bgcolor=#cccccc><td align=center><b>Student No</b></td><td align=center><b>Unit Id</b></td><td align=center><b>Question Id</b></td><td align=center><b>Answer</b></td></tr>";

while($row = mysql_fetch_array($mysqlquery)) {
$myData .= '
<tr>
<td align=center>'.$row[studentno].'</td><td align=center>'.$row[unitid].'</td><td align=center>'.$row[questionid].'</td><td align=center>'.$row[answer].'</td>
</tr>';
}
$myDate .= "</table>";
    	     	     	  
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $myData;
fwrite($fh, $stringData);

fclose($fh);

$file="test.xls";
if (file_exists($file)):
  header("Content-Type: application/vnd.ms-excel");
  header("Content-Disposition: attachment;filename=".$file );
  header('Pragma: no-cache');
  header('Expires: 0');
  readfile($file);
endif;




?>

 

I have tried various paths, different path formats but nothing seems to be working. the file is located with all the other web page files. Is this the wrong location then where is the right one and how do I code that path in php.

 

Please help!

ok basically i have now got a table displaying in my browser rather than an excel file download. It appeared I just had the wrong path set.  ideally I want this file to automatically download as it does on my localhost with the same code and not show in the browser. any help?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.