I am storing files into an image field in SQL server storing the string in hex after converting into using:
unpack("H*hex", $datastring);
If I read from the database and write to a file, then open the file locally I can open it up just fine. But if I try to send the file as an attachment to the browser the file becomes corrupted and unreadable. Currently, the files in question are PDF and MSWord documents.
I am setting content-type to the appropriate MIME type for the files and setting content-disposition to attachment. I've tried various ways of streaming the file including dumping the string directly from the database and writing first to a file then reading the file (either line by line or with readfile()).
I am using a slightly customized version of the Zend framework, but I'm not sure if that is causing any issues. What should I do to send files to the browser?
How do you serve them?, in theory if you are using MVC, you need to disable the view at the end of your controller to avoid extra content being inyected at the bottom of your file.
Related
I am using a library to read XLS files which internally uses PHP's zip_open() function. When creating the files locally and then uploading to my test server everything works fine. However, when I use the XLS files downloaded from a website (normal download via browser), it does not work, instead returning Error 19 meaning that the file is not seen as a zip file, which is incorrect. Excel opens the file without problems. If I re-save the file locally as an XLSX file and then upload it, I get the same error (in this instance the file is opened by the PHP's ZipArchive class). Any ideas what the reason could be? I checked that the files are not read only, possibly some Unix permissions could be set that are not displayed in Windows? (Doubt this, as the error code indicates that the file could be accessed, but could not be identified as XLS)
Using:
Apache under Windows (WAMP)
PHP 5.4.12
It seems I had misread a line of code, the zip check is only done to determine if the XLS file is an incorrectly named XLSX file. The problem with the XLS file is that it returns no sheets when parsing, I need to look into this still. I do not know why saving the XLS file as an XLSX file (using Excel) results in an incorrect ZIP archive though, but guessing it is related.
How would i generate a file of all of the following types:
.js
.css
.html
I would also need to be able to read the file and overwrite it somehow.
Is this possible in anyway?
All of these files are just text files with different file extensions so if I could maybe make a text file and change the file extension?
As all of those are simply text files, all you need to do is create/open the file with the desired name and write text into them.
Check out some of the PHP file IO commands:
fopen()
fread()
fwrite()
fclose()
PHP is usually (often?) used as a templating language, where HTML "files" are generated "on-the-fly" and output directly to the browser.
I could be wrong, but it sounds like you are talking about generating files and saving them to disk to be served statically to the browser later. If that's the case, you're right - they are just text-files - so writing them as text-files with the relevant file extension should work.
This seems like it would be logistically complicated to manage as a site scales though. Also - if you're creating an web-interface to edit files on the server, while some CMS-es do this (Wordpress for example), it does come with a lot of security issues (allowing PHP unrestricted write access to your server is rarely a good move).
How could I extract the METADATA / METAINFO / everything that would describe the REMOTE FILE without downloading it in the first place?
do you have some snippets in PHP?
how about in cURL?
or any language?
thanks
You can't get all the metadata from a file without downloading the entire file, as you have no way of knowing how much of the file you need before you've captured its metadata block, some file formats store metadata at the end of the file (meaning you'll need the entire file anyway), some files don't have any metadata embedded in them at all, and so on.
If you do a HEAD request, you will usually be able to get some basic metadata on the file in question in the form of the content-type and content-length headers returned by the server, but this is typically limited to the MIME type and the file size (and if you're dealing with a script that serves the file and that script doesn't set the necessary headers, you might not even get that).
I'm building a web app with PHP that uses Excel Writer (XML) for PHP to create an Excel file that the user can download. I've taken a look at the source code for the library, and all it does is echo the generated XML to standard output. Although the file being generated is an XML file, I specifically give it a name with a ".xls" extension so that after it's been downloaded the end-user can double-click it and it will launch Excel on their system and open the file successfully.
This works correctly when downloading the file with Firefox, Chrome and IE, but not with Safari. For example, if I set the name of the file to be 'File.xls', this is the name of the file that gets downloaded. But with Safari the name of the file ends up being 'File.xls.xml'.
The server is running Apache on Mac OS X. I thought that might matter as I'm guessing that the problem has to do with MIME types on the server, but I'm not at all sure. Perhaps I can do something with the link that appears on the page, or perhaps I need to edit a configuration file for Apache? Any help is appreciated.
You might be right, it could just be the MIME type that needs setting.
Try putting
header("Content-Type: application/vnd.ms-excel");
before the output code, that should force the browser to see the data as an Excel file.
If the code is setting Content-Type already and it's conflicting, then search the script for "Content-Type" and try changing it.
Also, if it's an XLSX file, then there's a different MIME type - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
I am having problems with reading html files in php.
When you try to open html file with Arabic name using fopen(), you get an error saying the file doesn't exist.
Is there any way to make this working?
You most likely have conflict between the file system encoding and your application encoding, or some file permissions issue.
(just a guess, too few data to judge)