Архив

Публикации с меткой ‘PHP’
21 октября 2021 Нет комментариев

При:

$cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities');

Ошибка:

<br />
<b>Warning</b>:  file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />
<br />
<b>Warning</b>:  file_get_contents(): Failed to enable crypto in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />
<br />
<b>Warning</b>:  file_get_contents(https://site.su/ajax.php?type=cities): failed to open stream: operation failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />

Решение:

$arrContextOptions=array(
	"ssl"=>array(
		"verify_peer"=>false,
		"verify_peer_name"=>false,
	),
);
$cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities',false,stream_context_create($arrContextOptions));
Categories: PHP Tags:
6 августа 2021 Нет комментариев

При ошибке

Access to XMLHttpRequest at 'https://site1.ru/api.php' from origin 'https://site2.ru' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

добавить на отдающий данные сайт (в примере site1.ru) заголовок

header('Access-Control-Allow-Origin: *');
Categories: PHP Tags: ,
3 августа 2021 Нет комментариев

Массив:

print_r($result_price);
Array
(
    [econom_mag] => 1800
    [econom] => 510
    [def] => 870
)

Ищем ключ минимального значения массива:

$min_key=array_keys($result_price,min($result_price));
print_r($min_key);
Array
(
    [0] => econom
)
Categories: PHP Tags:

javascript:

function canUseWebp(){
	let elem=document.createElement('canvas');
	if(!!(elem.getContext&&elem.getContext('2d'))){
		return elem.toDataURL('image/webp').indexOf('data:image/webp')==0;
	}
	return false;
}
window.onload=function(){
	let images=document.querySelectorAll('[data-bg]');
	for(let i=0;i<images.length;i++){
		let image=images[i].getAttribute('data-bg');
		images[i].style.backgroundImage='url('+image+')';
	}
	let isitFirefox=window.navigator.userAgent.match(/Firefox\/([0-9]+)\./);
	let firefoxVer=isitFirefox?parseInt(isitFirefox[1]):0;
	if(canUseWebp()||firefoxVer>=65){
		let imagesWebp=document.querySelectorAll('[data-bg-webp]');
		for(let i=0;i<imagesWebp.length;i++){
			let imageWebp=imagesWebp[i].getAttribute('data-bg-webp');
			imagesWebp[i].style.backgroundImage='url('+imageWebp+')';
		}
	}
};

html:

<div class="item" style="background-image:url('/img/banners/banner2.webp');" data-bg="/img/banners/banner2.jpg" data-bg-webp="/img/banners/banner2.webp"></div>

пример программного вывода:

function attr_background_image($image,$dir,$add=''){
	$html='';
	$image_path=ROOT_HTTP.$dir.$image;
	$image_pathinfo=pathinfo($image_path);
	$ext=($image_pathinfo['extension']=='jpg')?'jpeg':$image_pathinfo['extension'];
	if(USE_WEBP==1&&$ext!='svg'&&file_exists(ROOT_DIR.$dir.$image_pathinfo['filename'].'.webp')){
		$html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp\');" data-bg="'.ROOT_HTTP.$dir.$image.'" data-bg-webp="'.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp"';
	}
	else{
		$html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image.'\');'.$add.'"';
	}
	return $html;
}
$block_bg='';
if($block['photo']||$block['bgcolor']){
	if($block['photo']){
		$bgcolor='';
		if($block['bgcolor']){
			$bgcolor.='background-color:'.$block['bgcolor'].';';
		}
		$block_bg=attr_background_image($block["photo"],IMAGES_BLOCKS_DIR,$bgcolor);
	}
	elseif($block['bgcolor']){
		$block_bg.=' style="';
		$block_bg.='background-color:'.$block['bgcolor'].';';
		$block_bg.='"';
	}
}

или

<div class="item"<?=attr_background_image($item["photo"],IMAGES_BANNERS_DIR)?>>

https://webinmind.ru/javascript/webp-to-background-image

Categories: Javascript, PHP Tags: , ,
function write_ini($array,$file){
	$res=array();
	foreach($array as $key=>$val){
		if(is_array($val)){
			$res[]="[$key]";
			foreach($val as $skey=>$sval){
				$res[]="$skey = ".(is_numeric($sval)?$sval:'"'.$sval.'"');
			}
		}
		else{
			$res[]="$key = ".(is_numeric($val)?$val:'"'.$val.'"');
		}
	}
	safefilerewrite($file,implode("\r\n",$res));
}
function safefilerewrite($fileName,$dataToSave){
	if($fp=fopen($fileName,'w')){
		$startTime=microtime(TRUE);
		do{
			$canWrite=flock($fp,LOCK_EX);
			if(!$canWrite){
				usleep(round(rand(0,100)*1000));
			}
		}
		while((!$canWrite)and((microtime(TRUE)-$startTime)<5));
		if($canWrite){
			fwrite($fp,$dataToSave);
			flock($fp,LOCK_UN);
		}
		fclose($fp);
	}
}
$ini=parse_ini_file($_SERVER['DOCUMENT_ROOT']."/file.ini");
$ini['key']='value';
write_ini($ini,$_SERVER['DOCUMENT_ROOT']."/file.ini");
Categories: PHP Tags:
19 апреля 2021 Нет комментариев
function get_file_size($bytes){
	if($bytes<1000*1024){
		return number_format($bytes/1024,2)."KB";
	}
	elseif($bytes<1000*1048576){
		return number_format($bytes/1048576,2)."MB";
	}
	elseif($bytes<1000*1073741824){
		return number_format($bytes/1073741824,2)."GB";
	}
	else{
		return number_format($bytes/1099511627776,2)."TB";
	}
}
Categories: PHP Tags:
31 марта 2021 Нет комментариев
//список
print_r(glob(ROOT_DIR.RESIZE_CACHE_DIR."*".$fname));
//удалить по маске
foreach(glob(ROOT_DIR.RESIZE_CACHE_DIR."*".$fname) as $file){
	unlink($file);
}
Categories: PHP Tags: