Hello,

I have a page that I created whose purpose is to generate an excel file using things such as fopen to create a file. the page is working without problem when doing everything on my local server (xampp), but when I try to use the new page on my website ,it is not working at all, there seems to be an issue with the website creating a file on local computers. Does anyone have an idea on why this is happening and how to resolve this?

Thank you

    Could be a number of things... Directory permissions on the server, local path vs server path, etc, etc....

    Are you getting an error?

    Can you post your code?

      Hello,

      No I am not getting any errors. Everything seems to work fine until it reaches the moment where the program should generate the excel file then nothing happens (a success message should show but it is not showing and the file is not generated).

      here is the code involved, and don't put too much emphasis on the Spreadsheet/Excel/Writer.php, because in order to narrow the problem I tried a simple code with fopen, and it still did not work on the website:

      $directory="C:/Documents and Settings/phpLuver/";
      
      $myFile = $_POST['fileName2'];
      $zero=0;
      $one=1;
      $two=2;
      $three=3;
      //check if file name already exists or not.
      if (file_exists($directory.$myFile.".xls")) {
          $errors[]="the file: ".$myFile. " already exists, please enter a new name";
      	echo '<h1 id="mainhead">Error!</h1>
      	<p class="error"><font color="red" size="+1">The following error(s) occurred:<br />';
      	foreach ($errors as $msg) { /* Print each error. */
      		echo " - $msg<br />\n";
      	}
      	echo '</p><p>Please click the button to fix your errors and try again.</p></font>';
      
      echo '<br><br><input type=button value="Back" onClick="history.go(-1)">';	
      }else{
      
      chmod($directory.$myFile.".xls",'0777');
      require_once 'Spreadsheet/Excel/Writer.php';
      $counter=count($myarrayUpdate);
      
      $o=5;
      $counted=1;
       while($o<$counter)
       {
      	 if(!empty($myarrayUpdate[$o-1]))
      	  $myarrayUpdate[$o-1]=$counted;
      	 if($myarrayUpdate[$o]=='N')
      	 $myarrayUpdate[$o]='';
      	 $o=$o+4;
      
      
       $counted++;
       }
      
      
      // We give the path to our file here
      
      
      $workbook = new Spreadsheet_Excel_Writer($directory.$myFile.".xls");
      
      $myDate= date("m.d.y");
      $ei=0;
      $worksheet =& $workbook->addWorksheet('My first worksheet');
      $format_bold =& $workbook->addFormat();
      $format_column =& $workbook->addFormat(); 
      $format_merge =& $workbook->addFormat();
      $format_right =& $workbook->addFormat();
      $format_cell =& $workbook->addFormat();
      
      $format_merge->setAlign('merge');
      
      $format_right->setAlign('right');
      $format_right->setAlign('merge');
      
      $format_merge->setSize(15);
      $format_right->setSize(10);
      $format_bold->setBold();
      $format_cell->setAlign('center');
      //$format_bold->setBgColor('blue');
      $worksheet->setColumn(0,0,5);
      $worksheet->setColumn(1,1,20);
      $worksheet->setColumn(2,3,20);
      
      $j=0;
      
       $worksheet->write(0, 0, 'MY DELIVERY LOG',$format_merge);
      $worksheet->write(0, 1, '',$format_merge);
      $worksheet->write(0, 2, '',$format_merge);
      $worksheet->write(0, 3,'',$format_merge);
      $worksheet->setRow(0,30,0);
      
       $worksheet->write(1, 0, 'By:'. $User.'',$format_right);
      $worksheet->write(1, 1,'',$format_right);
      $worksheet->write(1, 2, 'Date:'.$myDate,$format_right);
      $worksheet->write(1, 3,'',$format_right);
      $worksheet->setRow(1,30,0);
      
      
      
       $worksheet->write(2, $zero, $myarrayUpdate[$j],$format_bold);
      $worksheet->write(2, $one, $myarrayUpdate[++$j],$format_bold);
      $worksheet->write(2, $two, $myarrayUpdate[++$j],$format_bold);
      $worksheet->write(2, $three,$myarrayUpdate[++$j],$format_bold);
      $worksheet->setRow(2,30,0);
      ++$j;
      
      
      
      
      for($i=3;$i<($counter/4)+3;$i++)
      {
      
      $worksheet->write($i, $zero, $myarrayUpdate[$j],$format_cell);
      $worksheet->write($i, $one, $myarrayUpdate[++$j],$format_cell);
      $worksheet->write($i, $two, $myarrayUpdate[++$j],$format_cell);
      $worksheet->write($i, $three,$myarrayUpdate[++$j],$format_cell);	
      $worksheet->setRow($i,30,0);
      
      $j++;
      }
      
      
      
      
      // We still need to explicitly close the workbook
      $workbook->close();
      }

        You'll need to adjust your directory path to reflect that of the web server you're uploading to.

        ie:

        //current - looks like it refers to the C drive on your local server/laptop/pc...
        $directory="C:/Documents and Settings/phpLuver/";
        
        //for example:
        $directory = '/www/htdocs/phpLuver/files/';
        

          The fact that you claim you're "not getting any errors" probably suggests that you a) haven't enabled display_errors and/or log_errors (and subsequently checked the appropriate log if the latter applies), and/or b) haven't set error_reporting to E_ALL (or greater).

          Also note that it might make more sense (and even things easier to manage) to use a relative path rather than an absolute path as you're currently doing.

            Jeeping88,

            I tried your suggestions but it did not help.

            I just recently started using php so bear with me,When you mentioned that I need to change my directory path to reflect that of the web server I am uploading to, for example $directory = '/www/htdocs/phpLuver/files/", I am not sure if I understand that. The purpose of the page is to generate a file that will be saved in $directory = "C:/Documents and Settings/user" or "/Documents and Settings/user", no matter who will be using the website (not just me) the page will save the excel file in that directory in his local computer. for example we may have 5 peoples in different locations of the country who want to use the site at the same time, when they will use website the page will create a file that will be saved on their local machine or in "/Documents and Settings/user". that is the reason why I set the $directory to C:/Documents and Settings/user", do you think that it still need be changed to reflect that of the web server I am uploading to?

            thank you,

            phpLuver

              Jeeping88,

              I tried your suggestions but it did not help.

              I just recently started using php so bear with me,When you mentioned that I need to change my directory path to reflect that of the web server I am uploading to, for example $directory = '/www/htdocs/phpLuver/files/", I am not sure if I understand that. The purpose of the page is to generate a file that will be saved in $directory = "C:/Documents and Settings/user" or "/Documents and Settings/user", no matter who will be using the website (not just me) the page will save the excel file in that directory in his local computer. for example we may have 5 peoples in different locations of the country who want to use the site at the same time, when they will use website the page will create a file that will be saved on their local machine or in "/Documents and Settings/user". that is the reason why I set the $directory to C:/Documents and Settings/user", do you think that it still need be changed to reflect that of the web server I am uploading to?

              thank you,

              phpLuver

                that could get pretty convoluted b/c you never know what people are going to be naming their directories or what drive they are going to be working on... perhaps they partition their drive....

                I would create the document, save it to a directory on your server and then give the user the option to download the created file to a directory of their choice.

                  phpluver;10995368 wrote:

                  no matter who will be using the website (not just me) the page will save the excel file in that directory in his local computer.

                  Whoa, wait a minute - you're talking about saving the file to a directory on the user's computer, not the server? Well let's start off by first agreeing on one thing: that's impossible.

                  Think about it; if it was possible to write a PHP script that had access to the user's hard drive, what's to stop some malicious websites from using PHP (or any other server-side language) to save trojans into your Windows system directories or various locations throughout your hard drive?

                  Even if you ignored the enormous security implications of the idea, just think about how it would occur. The only link between the webserver executing PHP and the end user's computer is through the HTTP connection established by the browser when it requested a given URL. How, then, could PHP open a file handle to that user's local computer? You can't magically hijack the HTTP protocol and somehow establish I/O handles across it.

                  Bonesnap;10995370 wrote:

                  do you think that it still need be changed to reflect that of the web server I am uploading to?

                  Again, yes, because the only place your server can write files to is... the server.

                  It would make more sense to either a) write the contents out to a temporary file on the server and provide a link for the user to access it (as suggested above), or b) output the appropriate HTTP headers to initiate a file download in the user's browser and simply output the contents of the file directly (e.g. you're skipping the step of saving the contents to a file on the server).

                  jeepin81 wrote:

                  that could get pretty convoluted b/c you never know what people are going to be naming their directories or what drive they are going to be working on

                  Agreed; my main desktop computer at home never runs Windows unless it's in a VM, so paths like "C:/(anything)" don't make any sense, nor does "/Documents and Settings/" exist.

                    4 days later

                    Thanks gentlemen,

                    you put me in the right path.

                      Write a Reply...