$files = array("images/1.jpg", "images/2.jpg", "images/3.jpg");
foreach($files as $file){
$temp = null;
$fp_in = fopen($file,'rb');
while(!feof($fp_in)){
$temp .= fread($fp_in,1024);
}
$output[$file] = $temp;
fclose($fp_in);
}
$output = implode('"',$output);
$zp = gzopen( 'sequences/backup.gz', "w9" );
gzwrite( $zp, $output );
gzclose( $zp );
The code above works but only one file is added to the archive. What is the best way to add multiple files to a archive using zLib?
require 'Tar.php';
$tar_object = new Archive_Tar("tarname.tar");
$tar_object->setErrorHandling(PEAR_ERROR_PRINT); // Optional error handling
$v_list = array("images/1.jpg", "images/2.jpg", "images/3.jpg");
$tar_object->createModify($v_list, "install");
function compress( $srcFileName, $dstFileName ){
// getting file content
$fp = fopen( $srcFileName, "r" );
$data = fread ( $fp, filesize( $srcFileName ) );
fclose( $fp );
// writing compressed file
$zp = gzopen( $dstFileName, "w9" );
gzwrite( $zp, $data );
gzclose( $zp );
echo 'success';
}
compress("tarname.tar","tarname.tar.gz");
unlink('tarname.tar');
Here is the updated code, with the help of Tim, thanks!
Related
I am making a page that will serve to decrypt .txt files that I will get from another website. I have been able to decrypt but only 1 to 1 so I am trying to decrypt 1 to 1 and put them all in a winrar. Unfortunately I'm having an error (post title) related to the "addFromString" method.
<?php
$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE);
$directory = 'uploadFiles/';
foreach (glob("uploadFiles/*.txt") as $file) {
$decrypted = decrypt_file($file,'pass');
$zip->addFromString($file, $decrypted);
}
if($res !== TRUE){
echo $res;
}
$zip->close();
function decrypt_file($file,$passphrase){
$iv = substr(md5("\x18\x3C\x58".$passphrase,true),0,8);
$key = substr(md5("\x2D\xFC\xD8".$passphrase,true).md5("\x2D\xFC\xD8".$passphrase,true),0,24);
$opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen($file,'rb');
stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
return $fp;
}
?>
I'm trying to build a document out of its various parts. Destination document does not get any content.
Can you possibly tell me what I'm doing wrong here ?
I have a begin.txt with HTML content and an end.txt which I want to keep having the new code (newarticle.txt) added to the beginning of so it flows forward every time a new addition is made. Then I want it all put together into articles.html.
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
$destination = fopen('articles.html', 'w+');
$begin = fopen('begin.txt','w+');
$end = fopen('end.txt','w+');
$currentend = file_get_contents('/end.txt');
$currentbegin = file_get_contents('/begin.txt');
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
// $content = <li><a href=$v1. PHP_EOL .$v2. PHP_EOL .$v3;
$build1 = <<<EOF
<li><a href="$v1">$v2</li>
<li>$v3</li>
EOF;
fwrite($file , $build1); //Now lets write it in there
fclose($file ); //Finally close our .txt
$file = "newarticle.txt";
$currentfile = file_get_contents($file);
$build2 = <<<EOF
$build1
$currentend
EOF;
ftruncate($end, 0); //Clear the file to 0bit
fwrite ($end , $build2);
fclose ($end );
$updatedend = 'end.txt';
$updatedendcontent = file_get_contents($updatedend);
ftruncate($destination, 0);
$build3 = <<<EOF
$currentbegin
$updatedendcontent
EOF;
fwrite ($destination , $build3);
fclose ($destination );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
Fixed it, this works
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
// $begin = fopen('begin.txt','w+');
$currentend = file_get_contents('end.txt');
$currentbegin = file_get_contents('begin.txt');
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
// $content = <li><a href=$v1. PHP_EOL .$v2. PHP_EOL .$v3;
$build1 = <<<EOF
<li><a href="$v1">$v2</li>
<li>$v3 by Accounting Today</li>
EOF;
fwrite($file , $build1); //Now lets write it in there
fclose($file ); //Finally close our .txt
//$file = "newarticle.txt";
$currentfile = file_get_contents('newarticle.txt');
$build2 = <<<EOF
$build1
$currentend
EOF;
$end = fopen('end.txt','w+');
ftruncate($end, 0); //Clear the file to 0bit
fwrite ($end , $build2);
fclose ($end );
//$updatedend = 'end.txt';
$updatedendcontent = file_get_contents('end.txt');
$build3 = <<<EOF
$currentbegin
$updatedendcontent
EOF;
$destination = fopen('articles.html', 'w+');
ftruncate($destination, 0);
fwrite ($destination , $build3);
fclose ($destination );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
I am using react to edit an image and send its final data using mycanvas.toDataURL() to php.
The API I am using says, instead of an image, I could although
[...] upload an image from a buffer (a string with binary)
Their example looks like this:
$sourceData = file_get_contents("example.jpg");
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();
Like I said, instead of an uploaded image, I have got a data URI looking like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4C2dj892d2dh98a2d...
How can I convert this data URI to a valid buffer to be used as $sourceData?
You can try converting the string to an image file and then send that file, you can use a code like this to do that:
function base64_to_image($base64_string, $output_file) {
$ifp = fopen( $output_file, 'wb' );
$data = explode( ',', $base64_string );
fwrite( $ifp, base64_decode( $data[ 1 ] ) );
fclose( $ifp );
return $output_file;
}
So, just call it like :
$file = base64_to_image('data:image/png;base64,iVBORw0...', 'myImage.png');
$sourceData = file_get_contents($file);
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();
Edit:
Alternatively(preferred) you can just use the decoded string as is instead of writing to a file and reading it. Like this:
function base64_to_image($base64_string) {
$data = explode( ',', $base64_string );
return base64_decode($data[ 1 ]);
}
And you can call it like this:
$sourceData = base64_to_image('data:image/png;base64,iVBORw0...');
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();
it's my first time doing signing of cert using openssl. Keep hitting the above error and tried realpath() and appending file:// but still can't get openssl to sign the profile. I don't understand how this works. Any insights would be appreciated.
Edit1: I'm not sure which file is the problematic one. The error messages wasn't specific enough. Is there a way to tell?
Code and screenshots below:
function signProfile()
{
$filename = "./template.mobileconfig";
$filename = realpath($filename);
$outFilename = $filename . ".tmp";
$pkey = dirname(__FILE__) . "/PteKey.key";
$pkey = realpath($pkey);
$certFile = dirname(__FILE__) . "/CertToSign.crt";
$certFile = realpath($certFile);
// try signing the plain XML profile
if (openssl_pkcs7_sign($filename, $outFilename, 'file://'.$certFile, array('file://'.$pkey, ""), array(), 0, ""))
{
// get the data back from the filesystem
$signedString = file_get_contents($outFilename);
// trim the fat
$trimmedString = preg_replace('/(.+\n)+\n/', '', $signedString, 1);
// convert to binary (DER)
$decodedString = base64_decode($trimmedString);
// write the file back to the filesystem (using the filename originally given)
$fh = fopen($filename, 'w');
fwrite($fh, $decodedString);
fclose($fh);
// delete the temporary file
unlink($outFilename);
return TRUE;
}
else
{
return FALSE;
}
}
Remove unwanted fields if not used in
openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());
Make sure file paths are correctly supplied.
require_once('variables.php'); //stores abs path to $tmpMobileConfig/$pteKeyPath/$CertToSignPath
$prepend = "file://";
$mobileConfig = realpath("./template.mobileconfig");
$pkey = $prepend . $pteKeyPath;
$pkey = str_replace('\\', '/', $pkey);
$certFile = $prepend . $CertToSignPath;
$certFile = str_replace('\\', '/', $certFile);
$isSignedCert = openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());
I have a line of data stored in $StripContent, when I echo $Stripcontent it shows
, , ,1 ,1415 ,Fietsen ,Omafietsen ,Avalon ,F 101 Oma Export 57cm Zwart , ,57 ,single speed remnaaf ,2017 ,21 ,249.00 ,135.00 ,19.50 ,8
However when I write $Stripcontent to the CSV file it only writes the first character instead of the whole line.
$Get_Content = file_get_contents($newname);
$StripContent = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",',', $Get_Content);
$file = $newname;
$content = file($file);
foreach($content as $lineNumber => &$lineContent) {
if($lineNumber == 0) {
$lineContent .= $StripContent . PHP_EOL;
}
}
$allContent = implode("", $content);
file_put_contents($file, $allContent);
You can do it more direct with the $Stripcontent variable direclty, whithout the foreach part, like:
$Get_Content = file_get_contents($newname);
$StripContent = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",',', $Get_Content);
$file = $newname;
$content = file($file);
file_put_contents($file, $Stripcontent);
This works if you want to store the stripContent string. I recomend you to wrap it between a try{...}catch() and check for folder permisions to avoid problems!
Or if you want to use the built-in function for CSV you can use fputcsv (offical documentation):
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
Hope it helps!