Solved

fopen, fread, fwrite fclose in PHP vs copy/robocopy

Posted on 2013-11-05
10
1,403 Views
Last Modified: 2013-11-11
Hi Experts,

I am wondering the mechanism behind the fopen, fread, fwrite, and fclose with PHP vs copy/robocopy command from window.

I can "copy" file either by fopen, fread... etc. and exec() by implmenting copy/robocopy into the PHP code.

I wish to know Pros and Cons of each and if there are restrictions with each process.

My goal is to copy DBF file from source location over to my destination. If there is a program that access the DBF, I wish  to know what is the safest way to do the file copy as in fopen or the copy/robocopy? Thanks
0
Comment
Question by:kisegi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39627096
I got a neglected question alert on this.  Here is what I would try first:

<?php file_put_contents('out.dbf', file_get_contents('in.dbf'));

Set up some automated verification tests so you can readily identify whether the copy worked by running the tests on both versions of the DBF and comparing the outputs.

If you want to learn more about PHP functions, they are all documented in the online man pages.  Example:
http://php.net/manual/en/function.fopen.php
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 39627130
Something to think about is also the memory used for the operation. I haven't done any testing but doing the copy in php may use up the php memory space where as spawning something like robocopy or any other copy executible would be outside of that.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39627165
@aarontomosky: Very good point.  Out of memory conditions would cause an immediate failure for file_get_contents().  Using a loop with fread(), fwrite() will get around that, if it is an issue.  Without knowing the size of the data base it's a little unpredictable, but very easy to test.  You can discern the amount of memory available to PHP with this script, shown here in its entirety.  Look for "memory_limit" in the output listing.
<?php phpinfo();

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 43

Expert Comment

by:Rob
ID: 39628456
I would certainly use robocopy as that is what it is designed to do. Not exactly what php is designed to do. Keep it simple and use the tools as they were designed.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39628642
Pros and cons have mostly been discussed.

Some more points:

It's less easy to verify the successful copy of a file, when shooting a copy job at windows robocopy.exe, also this is then limited to work on Windows. Robocopy can of course write a log or you may watch the destination folder, but with php fopen/wread/fwrite/fclose you surely know what's happening and when it's finished.

Then there also may be the timeout speaking for robocopy again. You can of course also use spawn shell script/command, when running PHP on linux.

Why not use ftp/ftps to put the file in the right place right away? Or what's the source of the file?

Anyway: The file type doesn't matter much. As it's DBF you may want to copy just certain data from it and then you better work with dbase/clipper/foxpro on the file instead of a simple full file copy.

Bye, Olaf.
0
 

Author Comment

by:kisegi
ID: 39629282
Hi Olaf,

I have to copy out the DBF file in order to use foxpro driver with odbc. If I try to read the data from DBF directly, I will have issue when another program is using DBF file. Also when I copy the DBF file while the other program wants to access the DBF, the other program may crash.

I am trying to find a better way which I can copy the data without crashing the other program.

If there is a good way to do so let me know. I've tried robocopy, xcopy, and copy which the other program will have issue if it tries to use that particular DBF file when I am copying it over.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39629487
Check out your EXCLUSIVE Setting in foxpro. Don't use a DBF exclusive and you can use it from many clients and applications.

Bye, Olaf.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39629500
You're either going to have to do this at an hour no one is going  be using the file or shutdown all services so that no one can use the file when you copy it.
Some backup programs make use of vss on Windows but even that can't back up a file that is locked constantly. It may appear to do so sometimes but the data integritywill most likely be compromised.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 39629548
Tagit has mentioned an important thing:

You can't copy a file which is locked constantly (EXCLUSIVE use does that), and also vice versa, you can't copy a file, which is locked by the application, even if just from time to time. And that is valid for any file and file type.

Since you post this in MS DOS, I assume your foxpro app is for DOS and was a single user application in the first place. You try to share data by copying it? Or do you need this to convert the copied DBF to newer version?

If a foxpro DOS developer could be sure the app and it's data (eg a POS system) would just be used by one user, he could use DBFs EXCLUSIVE which speeds things up. That was important in the DOS days.

The application most probably is not designed for multi user, even if you would put DBFs on a server share and map it to the drive letter the application is used to (and you're lucky if that is not C:), you can't enforce multi user mode to a non multi user application.

Then you're already doing the best thing you can do, copy the data, but neither xcopy, robocopy, nor PHP or other languages will do file access in a way disregarding file locks, the locks are done on the filesystem level/OS level. Robocopy is one of the best options, as it can repeat until it has success, nevertheless that only will be, if it gains access again. So you only can copy data without interruptions by the app and without interrupting the app during off hours, not during application use.

Bye, Olaf.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39640050
So what ended up being the solution?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question