files
object
%CSV
/phlo/resources/files/CSV.phlo
static
CSV :: __handle
line 9
CSV::$__handle 是对用于读取或写入位于指定路径和文件名的 CSV 数据的文件句柄的引用。
"CSV/$path$filename"method
%CSV -> __construct (string $filename, ?string $path = null)
line 10
通过从给定的文件名和可选路径(默认为'data')构造文件路径来初始化CSV对象。如果构造的文件可读,则调用objRead方法以读取CSV数据。
$path ??= data
$this->objFile = $path.strtr($filename, [slash => dot]).'.csv'
if (is_readable($this->objFile)) $this->objRead()readonly
%CSV -> objFile:string
line 16
将CSV文件转换为obj表示,以便在Phlo中更容易操作。
method
%CSV -> objRead
line 18
读取CSV文件并将其内容转换为对象数组,将标题映射到相应的行值。
$fp = fopen($this->objFile, 'r+')
$headers = str_replace([dq, cr, lf], void, fgets($fp))
$delimiter = substr_count($headers, comma) > substr_count($headers, semi) ? comma : semi
$headers = explode($delimiter, $headers)
while ($row = fgetcsv($fp, null, $delimiter)) $this->objData[] = array_combine($headers, $row)
fclose($fp)object
%DOCX
/phlo/resources/files/DOCX.phlo
method
%DOCX -> __construct (string $file)
line 10
通过打开指定的DOCX文件、从'word/document.xml'中提取文本并将其处理为段落来初始化DOCX对象。
$zip = new ZipArchive()
if ($zip->open($file) !== true) dx('error opening docx', $file)
$xml = $zip->getFromName('word/document.xml')
$zip->close()
if (!$xml) dx('error reading document.xml')
$text = preg_replace('/<\/w:p>/', "\n", $xml)
$text = strip_tags($text)
$text = html_entity_decode($text, ENT_QUOTES | ENT_XML1, 'UTF-8')
$this->text = trim(preg_replace('/[ \t]+/', ' ', $text))
$this->paragraphs = array_values(array_filter(explode("\n", $this->text), fn($p) => trim($p) !== void))static
DOCX :: toText (string $file):string
line 23
将DOCX文件的内容转换为纯文本格式。
(new static($file))->textobject
%file
/phlo/resources/files/file.phlo
static
file :: __handle
line 9
该表达式通过将'file/$file'与可选名称(如果提供)连接来构建文件路径。
"file/$file".($name ? "/$name" : void)method
%file -> __construct (public string $file, ?string $name = null, $contents = null, ...$args)
line 10
使用指定的文件名、可选名称和可选内容初始化文件对象,同时允许额外的参数用于对象导入。
$name && $this->name = $name
is_string($contents) && $this->write($contents)
$args && $this->objImport(...$args)method
%file -> append (string $data)
line 16
将指定的字符串数据附加到文件末尾,并确保在操作期间的独占访问。
file_put_contents($this->file, $data, FILE_APPEND | LOCK_EX)prop
%file -> basename
line 17
从给定的文件路径中返回文件的基本名称。
pathinfo($this->file, PATHINFO_BASENAME)method
%file -> base64
line 18
将文件的内容编码为 Base64 字符串。
base64_encode($this->contents)method
%file -> contents
line 19
将文件的全部内容读取为字符串。
file_get_contents($this->file)method
%file -> contentsINI (bool $parse = true)
line 20
读取文件的内容并将其解析为INI字符串,返回一个关联数组。$parse参数决定是否使用类型化或原始扫描INI数据。
parse_ini_string($this->contents, true, $parse ? INI_SCANNER_TYPED : INI_SCANNER_RAW)method
%file -> contentsJSON ($assoc = null)
line 21
将存储在contents属性中的JSON字符串解码为PHP变量,如果$assoc参数设置为true,则可选择返回关联数组。
json_decode($this->contents, $assoc)method
%file -> copy ($to)
line 22
将当前对象表示的文件复制到指定的目标路径。
copy($this->file, $to)method
%file -> created
line 23
返回文件创建的最后时间的Unix时间戳。
filectime($this->file)method
%file -> createdAge
line 24
根据文件的创建时间戳返回文件的年龄。
age($this->created)method
%file -> createdHuman
line 25
返回文件创建时间的人类可读表示。
time_human($this->created)method
%file -> curl ($type = null, $filename = null)
line 26
为cURL请求创建一个新的CURLFile对象,允许指定文件类型和文件名。
new CURLFile($this->file, $type, $filename)method
%file -> delete
line 27
如果指定的文件存在,则删除该文件,并返回指示操作成功或失败的调试消息。
first($deleted = $this->exists && unlink($this->file), debug($deleted ? "Deleted $this->basename" : "Could not delete $this->basename"))method
%file -> exists
line 28
检查指定的文件在文件系统中是否存在。
file_exists($this->file)prop
%file -> ext
line 29
使用PHP的pathinfo函数从存储在'name'属性中的文件名中检索文件扩展名。
pathinfo($this->name, PATHINFO_EXTENSION)prop
%file -> filename
line 30
从存储在 'file' 属性中的文件路径中提取文件名。
pathinfo($this->file, PATHINFO_FILENAME)method
%file -> getLine
line 31
从文件指针中获取一行,失败时返回 false,成功时返回去除尾部空白的行。
($line = fgets($this->pointer)) === false ? false : rtrim($line)method
%file -> getLength (int $length)
line 32
通过从文件指针读取指定数量的字节来获取文件的长度。
fread($this->pointer, $length)method
%file -> is (string $file)
line 33
检查指定的文件是否与当前文件实例匹配。
$file === $this->filemethod
%file -> md5
line 34
计算指定文件的MD5哈希值。
md5_file($this->file)prop
%file -> mime
line 35
根据文件名返回文件的MIME类型。
mime($this->name)method
%file -> modified
line 36
返回指定文件的最后修改时间,格式为 Unix 时间戳。
filemtime($this->file)method
%file -> modifiedAge
line 37
根据文件最后修改的时间戳返回文件的年龄。
age($this->modified)method
%file -> modifiedHuman
line 38
返回文件最后修改时间的可读表示。
time_human($this->modified)method
%file -> move ($to)
line 39
将当前文件移动到由 $to 指定的新位置,并在成功时更新文件引用。
rename($this->file, $to) && $this->file = $toprop
%file -> name
line 40
这将获取对象所表示文件的基本名称,即不带任何目录路径的文件名。
$this->basenamemethod
%file -> output ($download = false)
line 41
输出文件的内容,如果指定,则允许下载选项。
output($this->contents, $this->name, $download)prop
%file -> path
line 42
这会获取指定文件的目录路径并在其后附加一个斜杠。
realpath(pathinfo($this->file, PATHINFO_DIRNAME)).slashprop
%file -> pathRel
line 43
这个表达式通过检查文件路径是否以'app'开头来返回文件的相对路径,如果是,则去掉该前缀;否则返回原始文件路径。
str_starts_with($this->file, app) ? substr($this->file, strlen(app)) : $this->fileprop
%file -> pointer
line 44
以读写模式打开指定文件,返回指向文件资源的指针。
fopen($this->file, 'r+')method
%file -> readable
line 45
检查指定的文件是否可读。
is_readable($this->file)method
%file -> src
line 46
该表达式生成一个文件的数据URI,结合其MIME类型和base64编码的内容。
"data:$this->mime;base64,$this->base64"method
%file -> size
line 47
返回指定文件的大小(以字节为单位)。
filesize($this->file)method
%file -> sizeHuman (int $precision = 0)
line 48
将文件大小(以字节为单位)转换为人类可读的格式,并可选择小数位数的精度。
size_human($this->size, $precision)method
%file -> sha1
line 49
计算指定文件的SHA-1哈希值。
sha1_file($this->file)method
%file -> shortenTo (int $length)
line 50
将文件名缩短到指定长度,同时保留文件扩展名,如果名称被截断,则添加省略号。
strlen($this->name) <= $length ? $this->name : substr($this->name, 0, $length - strlen($this->ext) - 3).dot.dot.dot.$this->extmethod
%file -> title
line 51
从文件名中提取标题,通过将其转换为可读格式,替换下划线为空格并将首字母大写。
ucfirst(strtr(pathinfo($this->name, PATHINFO_FILENAME), [us => space]))method
%file -> token ($length = 20)
line 52
使用SHA-1哈希生成指定长度的令牌。
token($length, $this->sha1)method
%file -> type
line 53
通过提取第一个斜杠之前的子字符串,从 MIME 类型中提取文件类型。
substr($this->mime, 0, strpos($this->mime, slash))method
%file -> touch
line 54
创建一个新文件或更新由文件路径指定的现有文件的时间戳。
touch($this->file)method
%file -> writable
line 55
检查指定的文件是否可写。
is_writable($this->file)method
%file -> writeINI ($data, bool $deleteEmpty = false)
line 56
将数据写入INI文件格式。可选参数允许在设置为true时删除空节。
$this->write(!$deleteEmpty || $data ? loop($data, fn($value, $key) => $key.' = '.dq.strtr($value, [dq => bs.dq, lf => '\n']).dq, lf).lf : void, $deleteEmpty)method
%file -> writeJSON ($data, bool $deleteEmpty = false)
line 57
将提供的数据写入JSON文件,并可以选择在数据为空时删除该文件。
$this->write(!$deleteEmpty || $data ? json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : void, $deleteEmpty)method
%file -> writeJSONplain ($data, bool $deleteEmpty = false)
line 58
将提供的数据作为 JSON 字符串写入文件,基于 deleteEmpty 标志可选择性地删除空条目。
$this->write(!$deleteEmpty || $data ? json_encode($data) : void, $deleteEmpty)method
%file -> write (string $data, bool $deleteEmpty = false)
line 59
将指定的字符串数据写入文件,并可以选择在数据为空时删除该文件。
if (!$data && $deleteEmpty) return $this->delete
if ($written = file_put_contents($this->file, $data, LOCK_EX) !== false) debug('Written '.$this->basename.' ('.$this->sizeHuman.')')
else error('Could not write '.$this->file)
return $writtenmethod
%file -> objInfo
line 66
创建一个包含文件信息的对象,包括其名称、存在性,以及如果存在则可选的大小、创建日期、修改日期和MIME类型。
array_combine($keys = array_merge(['file', 'name', 'exists'], $this->exists ? ['sizeHuman', 'createdHuman', 'modifiedHuman', 'mime'] : []), loop($keys, fn($arg) => $this->$arg))object
%img
/phlo/resources/files/img.phlo
static
img :: detect ($data)
line 9
通过检查给定二进制数据的头部来检测图像格式,并返回相应的格式字符串。
$header = substr($data, 0, 12)
if (substr($header, 0, 3) === "\xFF\xD8\xFF") return 'jpg'
if (substr($header, 0, 8) === "\x89PNG\x0D\x0A\x1A\x0A") return 'png'
if (substr($header, 0, 6) === 'GIF87a' || substr($header, 0, 6) === 'GIF89a') return 'gif'
if (substr($header, 0, 4) === 'RIFF' && substr($header, 8, 4) === 'WEBP') return 'webp'
if (substr($header, 0, 2) === "BM") return 'bmp'
if (substr($header, 0, 4) === "\x49\x49\x2A\x00" || substr($header, 0, 4) === "\x4D\x4D\x00\x2A") return 'tiff'static
img :: search ($search)
line 19
此函数根据查询在Google上搜索图像,检索图像数据,并返回从顶部结果中随机选择的base64解码图像源。
$q = strtr($search, [dash => '+'])
$DOM = HTTP("http://images.google.it/images?as_q=$q&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y", agent: true)
$sources = regex_all('/var s=\'data:image\/jpeg;base64,([^\']{1000,})/', $DOM)[1]
usort($sources, fn($a, $b) => strlen($b) <=> strlen($a))
$sources = array_slice($sources, 0, 5)
shuffle($sources)
$source = current($sources)
$source = base64_decode($source)
return $sourcestatic
img :: __handle
line 31
这从指定的文件路径中检索图像资源。
"img/$file"method
%img -> __construct (public string $file)
line 32
使用公共字符串属性$file初始化img对象,该属性表示图像的文件路径。
prop
%img -> src:GdImage
line 34
此函数从包含图像数据的字符串创建图像资源,通常是从文件加载的。
imagecreatefromstring(file_get_contents($this->file))prop
%img -> width
line 35
获取由`src`属性指定的图像资源的宽度。
imagesx($this->src)prop
%img -> height
line 36
返回由`src`属性指定的图像的高度。
imagesy($this->src)method
%img -> scale ($width = null, $height = null, $crop = false)
line 38
将图像缩放到指定的宽度和高度,基于提供的参数可选择性地裁剪。
if (!$width && !$height) return $this
$srcW = $this->width
$srcH = $this->height
$doCrop = ($crop && $width && $height)
if ($width && $height) $scale = $doCrop ? max($width / $srcW, $height / $srcH) : min($width / $srcW, $height / $srcH)
elseif ($width) $scale = $width / $srcW
else $scale = $height / $srcH
if ($scale >= 1) return $this
$scaledW = (int)round($srcW * $scale)
$scaledH = (int)round($srcH * $scale)
$destW = ($width && $height && $doCrop) ? (int)$width : $scaledW
$destH = ($width && $height && $doCrop) ? (int)$height : $scaledH
$offsetX = 0
$offsetY = 0
if ($width && $height && $doCrop){
$offsetX = (int)-round(($scaledW - $destW) / 2)
$offsetY = (int)-round(($scaledH - $destH) / 2)
if ($crop === 'top') $offsetY = 0
elseif ($crop === 'bottom') $offsetY = (int)-($scaledH - $destH)
}
$destImg = imagecreatetruecolor($destW, $destH)
imagealphablending($destImg, false)
imagesavealpha($destImg, true)
imagecopyresampled($destImg, $this->src, $offsetX, $offsetY, 0, 0, $scaledW, $scaledH, $srcW, $srcH)
$this->src = $destImg
$this->width = $destW
$this->height = $destH
return $thismethod
%img -> ext ($file = null)
line 69
返回指定文件的小写文件扩展名。如果未提供文件,则使用实例的默认文件。
strtolower(pathinfo($file ?? $this->file, PATHINFO_EXTENSION))method
%img -> source ($format = null)
line 71
以指定格式检索图像的源,如果未提供,则使用默认格式,通过捕获write方法的输出。
ob_start()
$this->write($format)
return ob_get_clean()method
%img -> save ($file = null)
line 77
如果提供了文件路径,则将图像保存到指定的文件路径;否则,将图像写入默认位置。
$file && $this->file = $file
return $this->write(null, $this->file)method
%img -> write ($format = null, $file = null)
line 82
将图像以指定格式写入文件,如果未提供格式,则默认为JPEG。支持的格式包括PNG、GIF、WebP和JPEG。
$format ??= $this->ext()
if ($format === 'png') return imagepng($this->src, $file, 8)
if ($format === 'gif') return imagegif($this->src, $file)
if ($format === 'webp'){
imageistruecolor($this->src) || imagepalettetotruecolor($this->src)
return imagewebp($this->src, $file)
}
return imagejpeg($this->src, $file, 85)object
%INI
/phlo/resources/files/INI.phlo
prop
%INI -> objFile:string
line 9
INI->$objFile 用于创建或访问 INI 文件对象,以读取或写入配置设置。
static
INI :: __handle
line 11
INI::$__handle 获取指定 INI 文件的句柄,可根据提供的参数选择性解析。
"INI/$path$filename".(!$parse ? '/0' : void)method
%INI -> __construct (string $filename, ?string $path = null, bool $parse = true)
line 12
使用指定的文件名和可选路径初始化一个INI对象,并在文件可访问时读取该文件。
$path ??= data
$this->objFile = $path.strtr($filename, [slash => dot]).'.ini'
if (is_readable($this->objFile)) $this->objRead($parse)method
%INI -> objRead ($parse = true)
line 18
读取一个 INI 文件并将其转换为 obj,值可以选择性地解析为各自的类型。
last($this->objData = parse_ini_file($this->objFile, true, $parse ? INI_SCANNER_TYPED : INI_SCANNER_RAW), $this->objChanged = false, $this)method
%INI -> objWrite
line 19
将当前对象数据以INI文件格式写入,确保特殊字符被正确转义,并在写入操作期间锁定文件。
file_put_contents($this->objFile, loop($this->objData, fn($value, $key) => $key.' = '.dq.strtr($value, [dq => bs.dq, lf => '\n']).dq, lf).lf, LOCK_EX)method
%INI -> __destruct
line 21
INI->__destruct 方法检查对象是否已更改,并在必要时调用 objWrite 方法以处理对象销毁前的清理工作。
$this->objChanged && $this->objWrite()object
%JSON
/phlo/resources/files/JSON.phlo
static
JSON :: __handle
line 10
此函数根据提供的文件名和一个关联布尔标志构建JSON文件路径,决定路径的格式。
"JSON/$path$filename".(is_bool($assoc) ? slash.(int)$assoc : void)method
%JSON -> __construct (string $filename, ?string $path = null, $assoc = null)
line 11
根据指定的文件名和可选路径构造一个JSON对象,如果文件可读,则初始化该对象。
$path ??= data
$this->objFile = "$path$filename.json"
if (is_readable($this->objFile)) $this->objRead($assoc)readonly
%JSON -> objFile:string
line 17
将 JSON 字符串转换为 Phlo obj 以便进一步操作或处理。
method
%JSON -> objTouch
line 19
将 objChanged 属性设置为 true,表示对象已被修改。
$this->objChanged = truemethod
%JSON -> objRead ($assoc = null)
line 20
读取 JSON 文件并根据提供的参数将其转换为对象或关联数组。
last($data = json_read($this->objFile, $assoc), $this->objData = $assoc || is_array($data) ? $data : get_object_vars($data), $this->objChanged = false, $this)method
%JSON -> objWrite ($data, $flags = null)
line 21
将JSON数据写入对象文件, optionally使用指定的标志来修改写入行为。
first($written = json_write($this->objFile, $data, $flags), $written && $this->objChanged = false)method
%JSON -> __destruct
line 23
当对象被销毁时调用此方法,确保在对象从内存中移除之前,任何对对象数据的更改都被写入。
$this->objChanged && $this->objWrite($this->objData)object%PDF
/phlo/resources/files/PDF.phlo
static
PDF :: toText (string $file):string
line 9
使用 `pdftotext` 命令行工具将 PDF 文件转换为纯文本,并返回提取的文本。
$process = proc_open('pdftotext '.escapeshellarg($file).' -', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes)
if (!is_resource($process)) return null
fclose($pipes[0])
$text = stream_get_contents($pipes[1])
fclose($pipes[1])
$error = stream_get_contents($pipes[2])
fclose($pipes[2])
($code = proc_close($process)) && error("PDFToText Error: pdftotext command failed with code $code. Error: $error")
return rtrim($text, "\f")prop
%PDF -> title
line 21
生成具有指定标题的PDF文档。
nullprop
%PDF -> author
line 22
检索PDF文档的作者。
nullprop
%PDF -> subject
line 23
根据指定主题生成PDF文档。
nullprop
%PDF -> keywords
line 24
生成从PDF文档中提取的关键词列表。
nullprop
%PDF -> creator
line 25
PDF->$creator 属性用于获取 PDF 文档的创建者信息。
'Phlo '.phlo.' (https://phlo.tech/)'prop
%PDF -> filename
line 27
该项指定PDF资源的文件名为'Download.pdf'。
'Download.pdf'prop
%PDF -> mode
line 28
设置PDF处理的模式,其中'D'表示特定的操作模式。
'D'method
%PDF -> fromHTML ($HTML)
line 30
使用Mpdf库从提供的HTML内容生成PDF文档,在输出文件之前设置标题、作者、主题、关键字和创建者等各种元数据属性。
$mpdf = new \Mpdf\Mpdf
$this->title && $mpdf->SetTitle($this->title)
$this->author && $mpdf->SetAuthor($this->author)
$this->subject && $mpdf->SetSubject($this->subject)
$this->keywords && $mpdf->SetKeywords($this->keywords)
$this->creator && $mpdf->SetCreator($this->creator)
$mpdf->WriteHTML($HTML)
return $mpdf->Output($this->filename, $this->mode)object
%XLSX
/phlo/resources/files/XLSX.phlo
method
%XLSX -> __construct (string $file)
line 10
通过打开指定的Excel文件并提取其工作表、共享字符串和工作簿信息来初始化XLSX对象。
$sheets = []
$shared = []
$sheetNames = []
$zip = new ZipArchive()
if ($zip->open($file) !== true) dx('error opening zip', $file)
for ($i = 0; $i < $zip->numFiles; $i++){
$name = $zip->getNameIndex($i)
if ($name === false) continue
if (dirname($name) === 'xl/worksheets') $sheets[filter_var($name, FILTER_SANITIZE_NUMBER_INT)] = $zip->getFromIndex($i)
elseif ($name === 'xl/sharedStrings.xml'){
$xml = $zip->getFromIndex($i)
if (!preg_match_all('/<t[^>]*>(.*?)<\/t>/s', $xml, $m)) dx('error reading shared lib')
$shared = array_map(fn($t) => html_entity_decode($t, ENT_QUOTES | ENT_XML1, 'UTF-8'), $m[1])
}
elseif ($name === 'xl/workbook.xml'){
$xml = $zip->getFromIndex($i)
if (!preg_match_all('/<sheet[^>]*name="([^"]+)"[^>]*sheetId="([0-9]+)"/', $xml, $m)) dx('error reading workbook')
$sheetNames = $m[1]
}
}
$zip->close()
$toIndex = fn($letters) => array_reduce(str_split(strtoupper($letters)), fn($n, $c) => $n * 26 + ord($c) - 64, 0) - 1
$isShared = fn($attrs) => preg_match('/\bt="s"\b/', $attrs) === 1
foreach ($sheets AS $sheetID => $sheet){
$name = $sheetNames[$sheetID - 1] ?? 'Sheet '.$sheetID
if (!preg_match('/<row[^>]*>(.+)<\/row>/s', $sheet, $m)) dx('error parsing sheet')
$rowsXml = preg_split('/<\/row><row[^>]*>/', $m[1]) ?: []
$headerMap = []
$isHeader = true
foreach ($rowsXml AS $rowXml){
$rowXml = preg_replace('/<c([^>]*)\/>/', '<c$1></c>', $rowXml)
if (!preg_match_all('/<c r="([A-Z]+)[0-9]+"([^>]*)>(?:<f\b[^>]*\/?>)?(?:(?:<v>([^<]*)<\/v>)|(?:<is>.*?<t[^>]*>(.*?)<\/t>.*?<\/is>))?<\/c>/s', $rowXml, $mm)) dx('error parsing row', $rowXml)
if ($isHeader){
foreach (array_keys($mm[0]) AS $i){
$col = $toIndex($mm[1][$i])
$attrs = $mm[2][$i]
$valV = $mm[3][$i] ?? null
$valIS = $mm[4][$i] ?? null
$val = $valV !== null && $valV !== void ? $valV : ($valIS !== null && $valIS !== void ? html_entity_decode($valIS, ENT_QUOTES | ENT_XML1, 'UTF-8') : null)
$txt = $isShared($attrs) ? ($shared[$val] ?? null) : $val
$headerMap[$col] = $txt !== null && $txt !== void ? $txt : 'col'.$col
}
$isHeader = false
}
else {
$rowArr = []
foreach (array_keys($mm[0]) AS $i){
$col = $toIndex($mm[1][$i])
$attrs = $mm[2][$i]
$valV = $mm[3][$i] ?? null
$valIS = $mm[4][$i] ?? null
$val = $valV !== null && $valV !== void ? $valV : ($valIS !== null && $valIS !== void ? html_entity_decode($valIS, ENT_QUOTES | ENT_XML1, 'UTF-8') : null)
$key = $headerMap[$col] ?? 'col'.$col
$rowArr[$key] = $isShared($attrs) ? ($shared[$val] ?? null) : $val
}
$this->objData[$name][] = $rowArr
}
}
}