I have a zip file containing one file. I created the zip file and the file in it displays as expected. If I extract that file and copy it on my local WAMP server the destination file looks exatly as the original. But if I run the script on my Linux server, the destination file has line breaks after each line. So the contents of the original file are like this
line 1
line 2
line 3
while the contents of the destination file looks like this:
line 1
line 2
line 3
When the file is extracted it is left on disk. If I just issue a copy command to copy that file, the spacing in the destination file looks fine, on both servers. So it appears to be due to the copy command after the extract but how is that different from the straight copy? The code I'm using is below. I know I can read in file with ignore new lines and rewrite it but that seems wrong. Does anyone know how to fix this?
$zip = new ZipArchive;
if ($zip->open($myfile) === true) {
$filename = $zip->getNameIndex(0);
$zip->extractTo(".");
copy('myfile.php', 'some_dir/myfile.php');
$zip->close();
}
My Answer:
One possible solution is to use the `file_get_contents` and `file_put_contents` functions to read the file contents and write them to the destination file without adding any extra line breaks. Here's an updated version of your code that uses these functions:
php
$zip = new ZipArchive;
if ($zip->open($myfile) === true) {
$filename = $zip->getNameIndex(0);
$zip->extractTo(".");
$contents = file_get_contents('myfile.php');
file_put_contents('some_dir/myfile.php', $contents);
$zip->close();
}
This should preserve the original formatting of the file when copying it to the destination directory.
Rate this post
3 of 5 based on 7882 votesComments