简体   繁体   中英

Copy directories in PHP, using system vs not simple recursive PHP code

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.

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM