Reputation: 1915
I am using the following code to download an archived csv file and uncompress it:
$url="http://www.some.zip";
$target = 'data-' . md5(microtime()) . '.zip';
function download($src, $dst) {
$f = fopen($src, 'rb');
$o = fopen($dst, 'wb');
while (!feof($f)) {
if (fwrite($o, fread($f, 2048)) === FALSE) {
return 1;
}
}
fclose($f);
fclose($o);
return 0;
}
download($url,$target);
if ( file_exists($target) ){
echo "Download Successuful <br />";
$arc = new ZipArchive;
if (true !== $arc->open($target)) {
echo "Unzipping Failed <br />";
}else {
file_put_contents($out, $arc->getFromIndex(0));
echo "Unzipping Successuful <br />";
fclose($handle);
}
}else {
echo "Download Failed <br />";
}
However, on a second run, it does't do anything and I would like to overwrite the initial file with the newer file. (the CSV File)
How should I do that? The solution should take about the same time as the first download!
Upvotes: 0
Views: 1432
Reputation: 100
The easiest solution would be to first check if the file exists, then remove it.
function download($src, $dst) {
if(file_exists($dst)) unlink($dst);
$f = fopen($src, 'rb');
$o = fopen($dst, 'wb');
while (!feof($f)) {
if (fwrite($o, fread($f, 2048)) === FALSE) {
return 1;
}
}
fclose($f);
fclose($o);
return 0;
}
Upvotes: 1