Does php copy() function can convert a certain file format, let say for example file.pptx convert to file.odp, is it possible?

No, not through copy(), that will change only the extension. You can try PHPOffice/PHPPresentation:

For example:

$filename  = '/path/to/file.pptx';
$new_name  = '/destination/path/file.odp';
$phpPPTX   = \PhpOffice\PhpPresentation\IOFactory::load($filename);
$odpWriter = \PhpOffice\PhpPresentation\IOFactory::createWriter($phpPPTX, 'ODPresentation');
$odpWriter->save($new_name);

What is this means\PhpOffice\PhpPresentation\IOFactory::load and this \PhpOffice\PhpPresentation\IOFactory::createWriter.Does this will also work on .docx, .doc or .ppt, .xls and other office extensions. Please elaborate. Thank you

For excel or doc files refer to the other PHPOffice projects:

The code would be similar, where instead of calling PhpPresentation you would load PhpWord or PhpExcel. In the source code of the IOFactory class you can see which drivers are supported:

For more information look at their developer documentation.

IOFactory::load() is a static method to load a resource file, the createWriter() method is used, in this case, to convert the loaded file to another format, the segment \PhpOffice\PhpPresentation\ is the namespace in which the IOFactory class is loaded:

Does \PhpOffice\PhpPresentation\ is a directory or folder inbthe zip file that i have downloaded on github?

No, the namespace does not reflect a path into the filesystem. It's used to:

provide a way in which to group related classes, interfaces, functions and constants.

Check the link I provided in my previous post, it explains with examples how the namespace work.

In any case, if you load correctly the library you don't have to worry about the namespace unless you're using PHP lower than 5.3.

How and where did you get this stuff
\PhpOffice\PhpPresentation\IOFactory::load($file)

I am new here. please explain on an easy way. thank you

Sorry but sad to say that its too complicated to understand those namespace explanation in php.com becausr im not that pro in php

You have to require the autoload script. But I see that also the Common library is needed. The easiest way to install this package is through composer. Can you use it?

Composer is a packager manager for PHP, it gives you access to many libraries listed in:

Once installed it's easy to start using PHPOffice:

composer require phpoffice/phppresentation dev-master

This will install also the dependencies packages.

In order to do this manually create a src directory, then download both master zip files:

go into each src directory and copy the Common and the PHPPresentation folders into the src directory you generated, then use a script like this:

<?php

    require_once './src/PhpPresentation/Autoloader.php';
    \PhpOffice\PhpPresentation\Autoloader::register();

    require_once './src/Common/Autoloader.php';
    \PhpOffice\Common\Autoloader::register();

    $filename  = './samples/resources/Sample_12.pptx';
    $new_name  = './file.odp';
    $phpPPTX   = \PhpOffice\PhpPresentation\IOFactory::load($filename);
    $odpWriter = \PhpOffice\PhpPresentation\IOFactory::createWriter($phpPPTX, 'ODPresentation');
    $odpWriter->save($new_name);

You can see a live example here:

Which can be downloaded. To see it at work just type php index.php into the terminal, it will convert the sample file into odp.

Do i still need PhpPresentation-develop.zip that i have downloaded?

I tried all what youve said and it work. odp and odt file now can read by viewerJS.However theres a bug on the output. when i convert a docx file to odt which contains a table or shapes. Theres an error saying Division by zero in PhpWord\Writer\ODText\Style\Paragraph.php on line 41.

Also. some odp and odt contents are missing after converting. The odp and odt contents doesnt equal to the original file which is the pptx and docx.

Why is that happen?

Do i still need PhpPresentation-develop.zip that i have downloaded?

No, you should use the master version as this is usually the stable version. The develop version introduce some new features but is not stable and it can change, in general develop packages shouldn't be used in production, unless you understand which issues can produce.

However theres a bug on the output. when i convert a docx file to odt which contains a table or shapes. Theres an error saying Division by zero in PhpWord\Writer\ODText\Style\Paragraph.php on line 41.

For library related errors check their documentation:

And if it does not help, then submit a bug report (with full details) to their issues section:

Also. bulleted text from the docx filr are not written on the odp file

I see that php presentation and word has bug. May I ask you a question if there is some other way that i can view my office document file locally with my wamp server?? thank you

I see that php presentation and word has bug. May I ask you a question if there is some other way that i can view my office document file locally with my wamp server??

I don't know how this will performe in production servers, but as local solution you could install LibreOffice and perform an headless conversion from command line, something like:

exec("soffice --invisible --headless --convert-to odt file.docx");

More info here: https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters

For the options: just switch from -- to - in case of Windows environment.

In reference to the bugs I would submit them to PHPOffice, these libraries have a user large base, so it's possible that your issues have already be covered and you could get some help or temporary workarounds.

commented: perseverence +1 +15
commented: Headless thinking. What a concept. +1 +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.