Strstr return false instead of true - php

I want to open file, check if string deosn't exist in file write it.
Doing this:
$fp=fopen('categories.txt','a+');
$content=fread($fp,filesize('categories.txt'));
if(!strstr($content,$cat)){
fwrite($fp,','.$cat);
}
fclose($fp);
But I got repeating values in categories.txt after writing.
Only issue I can expect is encoding problem, but all files are utf-8 and in categories.txt I just have latin symbols and few symbols.
Any ideas where is the problem?

try like this.
$pos = strpos($content, $cat);
if($pos === false){
fwrite($fp,','.$cat);
}

Ok, I think problem in fopen. I changed to this, and code starts to work:
$content=file_get_contents('categories.dat');
$type=(string) $type;
$content=(string)$content;
if(!strstr($content,$type)){
file_put_contents('categories.dat',$content.','.$type);
}
Thanks for helps.

Related

Special characters encoding during CSV import

I have script that read *.CSV file and then export it content to MSSQL Database. Script is running only via CLI.
My problem is that this CSV file contains string with national characters like ą,ó,ż,ź,ś. For example i have word pracowników but in CLI i see word pracownikˇw.
My code
$handler = fopen($file, "r");
if ($handler !== false) {
while (($this->currentRow = fgetcsv($handler, 0, $this->csvDelimiter)) !== false) {
$row = $this->setHeaders(
$this->currentRow,
$this->config[$type]['columnMapping']
);
if ($row !== false) {
$this->dataImported[$type][] = $row;
}
}
fclose($handler);
}
What i tried
Using fgetcsv with setlocale or without - not working.
Replace fgetcsv with fgets and read each line via str_getcsv - not working.
Using utf8_encode for each row - not working.
Additional info
According to my PHP (PHP5.3) and few editors this file is encoded in ANSII, i tried to decoded it with iconv but all special characters are always replace with some strange symbols, like showed before.
On loop of $this->currentRow try to use for each element which has special char.
echo mb_convert_encoding($data[$c],"HTML-ENTITIES","UTF-8");

scandir in php with special (hungarian) characters

I've been trying to read (image) files a folder in my website. But, there's a problem! There are some special hungarian character (like: á,í,ú,ű,ó,é, etc...) in the name of the folder which I want to read. I've been trying many mode, but it doens't work. My php code:
<?php
$images = scandir("images/2005/avatás/");
foreach($images as $file) {
print($file);
}
?>
I've been trying to fix the problem, with mb_convert_encoding(), urlencode(), urldecode() functions, but these couldn't help me.
Have you got any idea?
Thanks!
iconv("windows-1254", 'UTF-8', $foldername)
Same problem same language, I assume this should work for you too
You could try using iconv()
<?php
$folder = iconv("windows-1251", "UTF-8", "images/2005/avatás/");
$images = scandir($folder);
foreach($images as $file) {
print($file);
}

searching a line in a txt file

I have a txt file in the server and it contains lines sth like that
one
two
three
four
five
i want to make a function that checks if a word exists in these lines.
any help is appreciated.
thank you
Here is how you may proceed with it:
$contents = file_get_contents('yourfile.txt');
$search_keyword = 'four';
// check if word is there
if (strpos($contents, $search_keyword) !== FALSE){
echo "$search_keyword was found !!";
}
else{
echo "$search_keyword was NOT found !!";
}
Does this really need to be done in php? You've just described the UNIX grep utility.
You could do it like:
$data = file($path_to_file,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (in_array($data,'word')) {
// do something
}
That is a simple hack but it should work.

php weird file_exists bug

Has anyone got any idea to why doesn't the following work ?
$file = 'images/thumbs/1%20-%20Copy.jpg';
if(!file_exists($file)){
die('NOT THERE');
}
echo 'Yes its there.';
The problem is with the spaces. I have checked the file exists,dbl checked n triple checked im going nuts. :(
Help
file_exists works on the file system and not via HTTP. So %20 will not be recognized as space but literally as %20; use spaces instead:
$file = 'images/thumbs/1 - Copy.jpg';
$file = rawurldecode('images/thumbs/1%20-%20Copy.jpg');
try these two
$file = 'images/thumbs/1\ -\ Copy.jpg';
$file = 'images/thumbs/1 - Copy.jpg';

PHP regular expression to match a filepath

Can someone please help me with this preg_match
if (preg_match('~[^A-Za-z0-9_\./\]~', $filepath))
// Show Error message.
I need to match a possible filepath. So I need to check for double slashes, etc. Valid file path strings should look like this only:
mydir/aFile.php
or
mydir/another_dir/anyfile.js
So a slash at the beginning of this string should be checked also. Please help.
Thanks :)
EDIT:
Also, guys, this path is being read from within a text file. It is not a filepath on the system. So hopefully it should be able to support all systems in this case.
RE-EDIT:
Sorry, but the string can also look like this too:
myfile.php, or myfile.js, or myfile.anything
How do I allow strings like this as well?? I apologize for not being too specific on this before...
Please notice that there are many types of possible file paths.
For example:
"./"
"../"
"........" (yes this can be a file's name)
"file/file.txt"
"file/file"
"file.txt"
"file/.././/file/file/file"
"/file/.././/file/file/.file" (UNIX)
"C:\Windows\" (Windows)
"C:\Windows\asd/asd" (Windows, php accepts this)
"file/.././/file/file/file!##$"
"file/.././/file/file/file!##.php.php.php.pdf.php"
All these file paths are valid. I can't think of a simple regex that can make it perfect.
Let's assume it's just a UNIX path for now, this is what I think should work for most cases:
preg_match('/^[^*?"<>|:]*$/',$path)
It checks all string for ^, *, ?, ", <, >, |, :(remove this for windows). These are all character that windows does not allow for file name, along with / and .
If it's windows, you should replace the path's \ with / and then explode it and check if it's absolute. Here is one example that working in both unix and windows.
function is_filepath($path)
{
$path = trim($path);
if(preg_match('/^[^*?"<>|:]*$/',$path)) return true; // good to go
if(!defined('WINDOWS_SERVER'))
{
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) define('WINDOWS_SERVER', false);
else define('WINDOWS_SERVER', true);
}
/*first, we need to check if the system is windows*/
if(WINDOWS_SERVER)
{
if(strpos($path, ":") == 1 && preg_match('/[a-zA-Z]/', $path[0])) // check if it's something like C:\
{
$tmp = substr($path,2);
$bool = preg_match('/^[^*?"<>|:]*$/',$tmp);
return ($bool == 1); // so that it will return only true and false
}
return false;
}
//else // else is not needed
return false; // that t
}
You can do:
if(preg_match('#^(\w+/){1,2}\w+\.\w+$#',$path)) {
// valid path.
}else{
// invalid path
}

Categories