Solved

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

Posted on 2013-11-05
10
1,298 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
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 38

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 108

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
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
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
Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

 

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 42

Expert Comment

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

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

910 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

21 Experts available now in Live!

Get 1:1 Help Now