0

I see there is not a built-in way to copy dirs in PHP. I saw Copy entire contents of a directory to another using php but I could not find any issue with system('cp -R src dest').

Is there an issue with using the above system call? I assume the only "problem" is it will create one extra process for a fraction of a second. Right?

I will clarify, in this scenario, I do not develop an off-the-shelf product that has to be platform independent, nor the src or dest are coming from user input, they are 100% clean. Only problem I see (as was pointed out below) is error handling.

Community
  • 1
  • 1
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

1 Answers1

1
  • Assuming src and dest are not hardcoded you have to carefully escape input against shell injections
  • This probably won't be cross platform compatible
  • Error handling will become more difficult as you probably have to parse shell output/stderr

Edit:

When assuming src & dest are clean and cross platform compatibility won't be an issue, then only problem that still stands is error handling - php copy() will explicitly return false in case of failure (for example incorrect filesystem permissions) and can be turned into custom recursive function.

Mayble Laravel FileSystem will help you - it is widely used and should be well tested: https://github.com/laravel/framework/blob/master/src/Illuminate/Filesystem/Filesystem.php

In order to see how to get stderr for shell commands, see: https://stackoverflow.com/a/2320835/294696

Community
  • 1
  • 1
Mikk
  • 2,209
  • 3
  • 32
  • 44
  • Your points make sense in a general scenario. In my case the src/dest are generated from the code and from numerical ids that where cleaned and I do not work on a platform independent/shelf product. I will modify my question to spot those points – Itay Moav -Malimovka Mar 13 '15 at 14:26