Solved

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

Posted on 2013-11-05
10
1,274 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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 38

Expert Comment

by:Aaron Tomosky
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kisegi
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
So what ended up being the solution?
0

Featured Post

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Join & Write a Comment

Suggested Solutions

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now