i want my PHPapp to read a file and tell me if it's a word, excel or powerpoint file. my first approach was to read the extension of the file. But microsoft has different file extension names, and i was also wondering about files uploaded by a mac (with no filename extension)
So, maybe the answer is using mimetypes. Do you know a better approach for this?
Yes, trying to determine the MimeType is your best bet short of just try/catching to load the files with PHPWord, PHPExcel and PHPowerpoint directly to see if they throw an exception (Mark Baker correct me please if they dont throw exceptions).
See my answer to
PHP how can i check if a file is mp3 or image file?
for various ways to detect the MimeType.
You can find a number of possible MimeTypes for Office documents at
http://filext.com/faq/office_mime_types.php and
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/87b9cd73-a41b-4fd0-94c7-dfe53e92947e
Related
I'm using a Wordpress plugin, wpdatatables, that uses PHPExcel to produce pdf, csv, and Excel file exports.
Anyway, when downloading an .xls file from my website, when I open it .. Excel gives me the error “The file format and extension of “blahblah.xls” don’t match. The file could be corrupted or unsafe. Unless you trust its source, don’t open it.”
Of course the file still opens fine, but I'd like to get rid of this error. One thing I noticed is the .CSV export is seemingly identical in all ways except the file extension, and opens without an error.
As someone who is not terribly familiar with PHP sadly, what direction should I look in to make PHPExcel produce .csv files only? Is there a specific function or directory in PHPExcel responsible for the Excel writer file output?
I would like some help to add a macro in an .xlsx file. I know that's not a good type of file, it should be an .xlsm file, but I have found Excel::Writer::XLSX in Perl that can add a macro to a new .xlsx file.
I want to open an existing .xlsx file (maybe with Excel::Reader::XLSX) and add vbaProject.bin into this file and save it (maybe as .xlsm). It has a function "add_vba_project()" but on Excel::Writer::XLSX which can't open existing file.
Has anyone found a solution in Perl or Python or PHP (PHPExcel for example) with or without vbaProject.bin?
I don't believe you'll find a pure PHP library capable of this, even PHPExcel doesn't support macros; but you can do so using COM with MS Excel, PUNO with Open/Libre Office, or Ilia's extension for the commercial libXL library
I followed this tut :
http://klewos.wordpress.com/2010/04/16/using-php-to-fill-a-word-document-quick-tip/
and all is fine till i open the downloaded .doc into libre office,
instead of outputing the doc content, it shows the full xml code as content !
Just note that i would prefer to create a PDF as final document but it seems easier with doc
I should miss a step with the xml to doc ?
if someone sees a better way to do this kind of thing (my base doc is 10 pages long so i don't really want to create a line by line pdf)
Anyhelp is welcome ;)
The article does mention that particular situation:
I’ve tried to open the Word 2003 XML document in OpenOffice.org 3.1.
Unfortunately, Writer wasn’t fooled by the .doc extension and opened
the document as plain text. Only after changing the document’s
extension to .xml, the editor opened it correctly. So, the documents
are portable after all.
I want to automate the following:
Once a day my cronjob starts a PHP script that obtains a zipped XML file from an URL.
What would be the best way to handle this? Is there any way to directly read the XML file from within the zip file?
Right now, i'm just downloading the zipped file to the server and manually unpacking it later that day.
Any ideas? All suggestions are very much welcome.
You can use PHP's ZipArchive coupled with cURL to download and read the zip file.
Also, the ZipArchive class has a method called getStream which allows you to then use fread to access the contents without explicitly extracting the file.
The only problem I see if that the zip does have to be saved somewhere for PHP's library to read it. But, given you're already doing this, it shouldn't be an issue.
If you need an example, leave me a comment and I can write on up.
There is a collection of zip-related functions that can be used in PHP.
The problem with these is that it requires the compressed file to exist on the server (not just loaded from an external server somewhere using, for example, $file = file($url);).
If you were to save the file to your server then you could use $zip = zip_open($filename) and zip_read($zip) to process the zip file.
'ImagickException' with message 'unable to open file /data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png'
Hi,
I got this message when I want to crete PDF file from the webpage, that png files. It throws error only when content of PDF contains some PNG file. When I remove all png files, that should be in pdf, everything is correct.
When I look in php info, I see png is supported format for Imagick. When I tried to find solution on google, it returned many of sites with the same error but almost no solution. the only solution I have found is to install another library to server. But I would prefer another solution if there is any.
thnaks for any advice
Tomas
ImageMagick can indeed take png files and convert it to pdf. Are you sure the file is in fact on the server at the correct place.
it feels like the inout command to ImageMagick may be incorrect. The path below looks weird.
data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png
^^^^^
Is this expected?
I can tell you more if you tell me the website. Otherwise, I would say its a bug in the website php/python script. It doesn't look like an ImageMagick issue to me.
The ImagickException error message indicates that Imagick (PHP class) is choking on the 'file://' prefix, which is a streamwrapper, fairly recent PHP addition. Imagick documentation is not clear on the use of stremwrappers, and it is most certain that Imagick does not support them. When it sees no '/' in the first byte of the file name, it assumes the file name is relative and pre-pends current directory to it, creating the monster string you pasted in the question.
Solution is to trim the 'file://' prefix on the file name you are sending to the Imagick. File name should be either absolute (start with /) or relative.
if (strpos('file://', $filename) === 0) {
$filename = substr($filename, 7);
}