I am importing data feed into mywebsite.My datafeed files are came from tar.gz format. The file size is is very large upto 10 gb. i need to split tar.gz file and then upload to database. How can i split tar.gz single file to multiple file. Please Guide me.
You can create several parts like this:
$fileName = 'something_big.tar.gz';
$fileSize = filesize($fileName);
$parts = 10;
$partSize = ceil($fileSize / 10);
$f_in = fopen($fileName, 'rb');
for ($i = 0; $i < $parts; ++$i) {
$f_out = fopen("$fileName.$i", 'wb');
stream_copy_to_stream($f_in, $f_out, $partSize);
fclose($f_out);
}
fclose($f_in);
Related
Say I'm uploading a chunked file and I have to recompose it. I know the total chunks and data from every iteration.
I founded code like this:
for ($i = 1; $i <= $num_chunks; $i++) {
$file = fopen($target_file.$i, 'rb');
$buff = fread($file, 2097152);
fclose($file);
$final = fopen($target_file, 'ab');
$write = fwrite($final, $buff);
fclose($final);
unlink($target_file.$i);
}
Apparently, the 2097152 value, has no meaning, at least to me. I read the php docs but couldn't understand too much. Could anyone explain me how I should choose that secon param of fread? And how the thing works?
The second parameter is the amount of data to read, as your reading this in one chunk you have to be sure that it is enough to process any chunk. The value you've set is 2MB, which may be enough, but you could change the code so that it reads it in smaller chunks and loops till the input is fully read.
I've also changed it to open the output file once and just write the contents as you go along...
$final = fopen($target_file, 'wb'); // Open for write and start from beginning of file
for ($i = 1; $i <= $num_chunks; $i++) {
$file = fopen($target_file.$i, 'rb');
while($buff = fread($file, 4096)) {
fwrite($final, $buff);
}
fclose($file);
unlink($target_file.$i);
}
fclose($final);
I have this file that gets downloaded at:
DownloadFile($reportDownloadUrl, $DownloadPath);
But it's a zip file. Inside of it, a CSV file gets created with a random name i.e random_name.csv
How do I extract this folder abc.zip in php and rename this file with random name to new_name.csv
Problem is that I can't use
$zip->renameName('currentname.csv','newname.csv');
since I don't have currentname.
This code inspects the file in zipLocation then iterates over them to check if there are csv files. If it finds something it copies inside the directory with its original name, then copies another copy with a new name.
$zipLocation = "path/to/file.zip";
$zip = new ZipArchive;
if ($zip->open($zipLocation) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
if (pathinfo($filename, PATHINFO_EXTENSION)=="csv"){
$fileinfo = pathinfo($filename);
copy("zip://".$zipLocation."#".$filename, "./newname.csv");
copy("zip://".$zipLocation."#".$filename, "./".$fileinfo['basename']);
}
}
$zip->close();
}
I have a quesion: How to count lines at a file over ftp?
Like: I have a file generated by C# shared by ftp. How to count the lines at php at my webserver?
Thanks, Jonathan
if you can access the file then can do this:
$file="yourfile.txt";
$count = 0;
$fp = fopen($file, "r");
while(!feof($fp)){
$line = fgets($fp);
$count++;
}
fclose($fp);
echo $count;
you can use count() also :
$count = count( file( $file));
but if the file is big you should avoid using this.
I've got a ZIP file sitting on my server. I want to unzip it and then save the completely file contents into just one variable.
I do NOT want to save the unzipped file on my server or on a visitor's computer. I just need all of the contents of that zipped file stored in a variable that I can play around with and eventually show on the screen. Every other solution I've found for this problem includes resaving the file in unzipped form.
How can I do this with get_file_contents, or any other function?
You can simply find out the file names within the ZIP Archive with PHPs ZipArchive for example. try this (have not tested it but you should be able to get it to work) :
$za = new ZipArchive();
$za->open('archive.zip');
$fileContents = array();
for( $i = 0; $i < $za->numFiles; $i++ ) {
$stat = $za->statIndex( $i );
$fp = $z->getStream($stat['name']);
if(!$fp) exit("failed\n");
$contents = '';
while (!feof($fp)) {
$contents .= fread($fp, 1000);
}
fclose($fp);
$fileContents[$stat['name']] = $contents;
}
Once you know the names of the files within the Zip you can also use something like this:
$path = sprintf('zip://%s#%s', $zipArchive, $fileNameInZipArchive);
$fileData = file_get_contents($path);
I want to take one text file, split it in half, and put one half in one file, then the remaining half in the next. How would one accomplish this?
an example would be: split.php?n=file.txt
$file = $_GET['n'];
$i = 1;
$fp = fopen("./server/php/files/".$file,'a+');
$fs = filesize("./server/php/files/".$file);
$lengthhalf = $fs / 2;
while(! feof($fp)) {
$contents = fread($fp,$lengthhalf);
file_put_contents('./server/php/files/[2]'.$file,$contents);
$i++;
}
This does the work, without reading the whole file (or half of it) at once in memory:
function split_in_halves($file, $half1, $half2) {
$size = filesize($file);
$fd = fopen($file, 'rb');
stream_copy_to_stream($fd, fopen($half1, 'wb'), $size/2);
stream_copy_to_stream($fd, fopen($half2, 'wb'));
}
split_in_halves('foo', '[1]foo', '[2]foo');