MOTIVECODEX Posted June 20, 2013 Share Posted June 20, 2013 (edited) Hello all! Sorry for the lame question, but I need to save XLS to my server, but how? Have removed parts that are not relevant to my question. This code saves the content into .xsl format and downloads it immediately to the desktop if run with your browser, but how can I save that file to the server? With something like file_put_contents()? But how? And does $files = header("Content-Disposition: attachment;filename=export_".date('n-j-Y').".xls "); even work? ( the `$files =` part ) Thanks for all the help! <?php // database connection etc goes here /* * START HEX writing to Excel format */ // Beginning of XLS file function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } // End of XLS file function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } // Writes text to cells function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } /* * Database connection goes here * * * * [removed] * * */ /* * Headers for the browser */ header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); $files = header("Content-Disposition: attachment;filename=export_".date('n-j-Y').".xls "); header("Content-Transfer-Encoding: binary "); // Start the file xlsBOF(); /* * To keep it in order * * [removed] * * */ // On first row $first = true; while( $qrow = mysql_fetch_assoc( $qr ) ) { if( $first ) { // [removed] } // goes through the data foreach( $qrow as $k => $v ) { // write it in the file // [removed] } // reset col and goto next row $col = 0; $row++; } xlsEOF(); exit(); ?> Edited June 20, 2013 by MOTIVECODEX Quote Link to comment https://forums.phpfreaks.com/topic/279383-save-xls-to-server/ Share on other sites More sharing options...
ginerjm Posted June 20, 2013 Share Posted June 20, 2013 XLS or xsl?? Do you mean "spreadsheet", aka, a client file? Saving a file to your server (aka, 'uploading') is simple. You can find many examples of how to code this. Try google. As for setting a php var with the header statement you proposed, I don't think that works. But I've been wrong many times. Quote Link to comment https://forums.phpfreaks.com/topic/279383-save-xls-to-server/#findComment-1437013 Share on other sites More sharing options...
MOTIVECODEX Posted June 20, 2013 Author Share Posted June 20, 2013 XLS or xsl?? Do you mean "spreadsheet", aka, a client file? Saving a file to your server (aka, 'uploading') is simple. You can find many examples of how to code this. Try google. As for setting a php var with the header statement you proposed, I don't think that works. But I've been wrong many times. Yes I mean xls, from excel. My code works perfect and it does what I want it to do, the only thing is that it does not save the file on my server. Quote Link to comment https://forums.phpfreaks.com/topic/279383-save-xls-to-server/#findComment-1437018 Share on other sites More sharing options...
ginerjm Posted June 20, 2013 Share Posted June 20, 2013 You need to develop a file upload html document. Google is your friend. Write something from what you find in you research and if you're then stuck, come back. Quote Link to comment https://forums.phpfreaks.com/topic/279383-save-xls-to-server/#findComment-1437024 Share on other sites More sharing options...
DavidAM Posted June 20, 2013 Share Posted June 20, 2013 If this script is running on your server and you want to save the file on that server, there is no upload involved. You have a couple of options: 1) Instead of echoing the data directly, collect it in a string. When you are done, write the string to a file and write it to the browser (i.e. echo). OR 2) Again, do not echo the data, write it directly to a file on the server. When you are done, send the file to the client (readfile). OR 3) Use the Output buffering functions (see ob_start) to collect what you are outputting and then grab the buffer, write it to a file, and echo it as well. Unless it is a HUGE amount of data, I would choose option 1. If the file is or can be very large, I would use option 2. I do not recommend option 3; eventhough it will be the easiest to implement into what you have, it can create issues and make it difficult to debug. $files = header(...) - The header function sends a header. It does not return a value, so $files there will be null and the header will be sent. The assignment is useless. What are you trying to accomplish there? You never use $files in the code you showed. Quote Link to comment https://forums.phpfreaks.com/topic/279383-save-xls-to-server/#findComment-1437030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.