PHP script who open and search data from .txt is:
function explodeRows($data) {
$rowsArr = explode("\n", $data);
return $rowsArr;
}
function explodeTabs($singleLine) {
$tabsArr = explode("\t", $singleLine);
return $tabsArr;
}
$filename = "/txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
for($i=0;$i<count($rowsArr);$i++) {
$lineDetails = explode("|",$rowsArr[$i]);
if ($kodas == $lineDetails[2]) {
$link3=$lineDetails[4];
echo "";
} }
fclose($handle);
It's works well, but now I transfer name.txt to another folder (folder name txt). How to make, first open this folder and search open name.txt
$filename = "txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
Related
I am creating a .eml file with the following code to create a "draft" email that can be open as draft in Outlook and ready to be sent.
I got it working 99%, but the email generated has the attachments with .dat extension.
For example, and excel file will be name myfile.xlsx.dat ; if I take the file and remove the .dat extension i can open it in Excel. Same thing for pdf attachment, gets renamed .dat, but remove the .dat and it opens as a valid PDF.
How can I get rid of of the .dat extension?
$part1["type"] = TYPEMULTIPART;
$part1["subtype"] = "mixed";
$fp = fopen($full_filename2, "r");
$contents = fread($fp, filesize($full_filename2));
fclose($fp);
$part2["type"] = TYPEAPPLICATION;
$part2["encoding"] = ENCBINARY;
$part2["subtype"] = "octet-stream";
$part2["description"] = basename($full_filename2);
$part2["contents.data"] = $contents;
$fp = fopen($full_filename3, "r");
$contents = fread($fp, filesize($full_filename3));
fclose($fp);
$part3["type"] = TYPEAPPLICATION;
$part3["encoding"] = ENCBINARY;
$part3["subtype"] = "octet-stream";
$part3["description"] = basename($full_filename3);
$part3["contents.data"] = $contents;
$fp = fopen($full_filename, "r");
$contents = fread($fp, filesize($full_filename));
fclose($fp);
$part5["type"] = TYPEAPPLICATION;
$part5["encoding"] = ENCBINARY;
$part5["subtype"] = "octet-stream";
$part5["description"] = basename($full_filename);
$part5["contents.data"] = $contents;
$part4["type"] = TYPETEXT;
$part4["subtype"] = "html";
$part4["description"] = "body";
$part4["contents.data"] = $email_body;
$body[1] = $part1;
$body[2] = $part2;
$body[3] = $part3;
$body[4] = $part4;
$body[5] = $part5;
$singlefile = uniqid(rand(), true) . '.eml';
$imapfile = 'C:\\wamp\\www\\billing\\emails\\'.$singlefile;
file_put_contents($imapfile , imap_mail_compose($envelope, $body));
I want to find a file with a wildcard in the same directory as my index.php is.
When I assign the $file_name manually with the name string, it works fine.
<?php
$file_name = glob("*.csv");
$handle = fopen($file_name, "r");
$file = fread($handle, filesize($file_name));
fclose($handle);
echo $file;
?>
The browser should output the content of the .csv file, like when I assign the $file_name manually.
you need a loop because glob() returns an array:
foreach (glob("*.csv") as $filename) {
$handle = fopen($filename, "r");
$file = fread($handle, filesize($filename));
fclose($handle);
echo $filename;
}
This script will find some images in working folder.
<?php
$workdir = getcwd(); // my working dir
$patternTofind = ".{jpg,gif,png}"; // Images example
$files = glob("$workdir*$patternTofind", GLOB_BRACE);
// Print result found
print_r($files);
?>
How would I save an array to a binary file and then read that binary file back to an array in php?
This is where I am so far, but it doesn't work:
$arr = array("key1"=>"val1","key2"=>"val2","key3"=>"val3");
//save file
$file_w = fopen('binint', 'w+');
$bin_str = pack('i', $arr);
fwrite($file_w, $bin_str);
fclose($file_w);
//load file
$filename = "file.bin";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
$newarr = unpack('i*', $contents);
print_r($newarr);
I'm trying to make it retrieve the image files on the server but it won't work if there is a space in the name of the image file .. for example there is a space between dead and air , even if I escape it after adding %20, the function returns an empty string .. but if it is a file with no space in the name like 'http://www.m.trialsite.com/images/thumb/Espresso.jpg'; It will work ! .. where am I going wrong ?
$filename = 'http://www.m.trialsite.com/images/thumb/dead air.jpg';
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$filename = str_replace(' ','%20',$filename);
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
$filename = str_replace(' ','%20',$filename);
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer; var_dump($buffer); exit;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
use preg_replace("/\s+/","_",$nome); to rename the files and then recovers it it will work
$directory = '/public_html/testfolder/';//example
if ($handle = opendir($directory)) {
while (false !== ($fileName = readdir($handle))) {
$newName = preg_replace("/\s+/","_",$fileName);
rename($directory . $fileName, $directory . $newName);
}
closedir($handle);
}
What if you are doing like this:
$filename = str_replace(' ','%20', 'http://www.m.trialsite.com/images/thumb/dead air.jpg');
Do you have any idea how to improve the performance by reading CSV file from a zip file?
Firstly it open the zip file, then put the data into a memory and then read it by fgetcsv
$zip = new ZipArchive();
if ($zip->open($fileName)) {
$info = $zip->statIndex(0);
$fp = $zip->getStream($info['name']);
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 2);
}
fclose($fp);
$zip->close();
}
$temp = fopen("php://memory", "rw");
fwrite($temp, $contents);
fseek($temp, 0);
while (($data = fgetcsv($temp, 0)) !== false) {
....
}
Quick check with php manual showed that this should work:
<?php
$fp = fopen('zip://test.zip#test', 'r'); // test name of file in archive
if (!$fp) {
exit("cannot open\n");
}
while (($data = fgetcsv($fp, 0)) !== false) {
...
}
fclose($fp);