Тэг "File Routines" (6)
Force the browser to download the file instead of opening it ... Показать код
$file_text = 'Some text here';
if (isset($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
Header('Content-Type: application/force-download');
else
Header('Content-Type: application/octet-stream');
Header('Accept-Ranges: bytes');
Header('Content-Length: ' . strlen($file_text));
Header('Content-disposition: attachment; filename="downloaded_file.txt"');
echo $file_text;
exit();
Extract .zip files, answered here: http://stackoverflow.com/questions/5653130/extract-zip-files-using-php ... Показать код
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Delphi: Version of EXE or DLL file
Function returns version of Exe/Dll file as "1.2.3.4" string ... Показать код
//------------------------------------------------------------------------------
// Returns version from file resource as string "1.2.3.4"
function GetFileVersion(const AFileName: string): string;
var
VerInfoSize: DWORD;
VerInfo: Pointer;
VerValueSize: DWORD;
VerValue: PVSFixedFileInfo;
Dummy: DWORD;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(AFileName), Dummy);
GetMem(VerInfo, VerInfoSize);
try
GetFileVersionInfo(PChar(AFileName), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
Result := IntToStr(dwFileVersionMS shr 16);
Result := Result + '.' + IntToStr(dwFileVersionMS and $FFFF);
Result := Result + '.' + IntToStr(dwFileVersionLS shr 16);
Result := Result + '.' + IntToStr(dwFileVersionLS and $FFFF);
end;
finally
FreeMem(VerInfo, VerInfoSize);
end;
end;
Delphi: Give a new name if the file already exists
Give a new name if the file already exists ... Показать код
function file_newname($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
$newname = $filename;
$counter = 0;
while (file_exists($newpath)) {
$newname = $name .'_'. $counter . $ext;
$newpath = $path.'/'.$newname;
$counter++;
}
return $newname;
}
Delphi: Get temp directory
Get Windows temp directory. Will return something like this: C:UsersMyUserNameAppDataLocalTemp ... Показать код
function GetTempDir: WideString; stdcall;
var
Buffer: array[0..MAX_PATH] of Char;
begin
GetTempPath((SizeOf(Buffer) div SizeOf(Char)) - 1, Buffer);
Result := Buffer;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
ShowMessage(GetTempDir);
end;
Delphi: Get the version of a file
Get the version of a file. The function was used from Delphi 7 to Delphi XE3. ... Показать код
function GetFileVersion(const FileName: string): string;
type
PDWORD = ^dword;
PLangAndCodePage = ^TLangAndCodePage;
TLangAndCodePage = packed record
wLanguage: word;
wCodePage: word;
end;
PLangAndCodePageArray = ^TLangAndCodePageArray;
TLangAndCodePageArray = array[0..0] of TLangAndCodePage;
var
loc_InfoBufSize: dword;
loc_InfoBuf: PChar;
loc_VerBufSize: dword;
loc_VerBuf: PChar;
cbTranslate: dword;
lpTranslate: PDWORD;
i: dword;
begin
result := '';
if (Length(FileName) = 0) or (not Fileexists(FileName)) then
exit;
loc_InfoBufSize := GetFileVersionInfoSize(PChar(FileName), loc_InfoBufSize);
if loc_InfoBufSize > 0 then
begin
loc_VerBuf := nil;
loc_InfoBuf := AllocMem(loc_InfoBufSize);
try
if not GetFileVersionInfo(PChar(FileName), 0, loc_InfoBufSize, loc_InfoBuf)
then
exit;
if not VerQueryValue(loc_InfoBuf, '\\VarFileInfo\\Translation',
Pointer(lpTranslate), dword(cbTranslate)) then
exit;
for i := 0 to (cbTranslate div SizeOf(TLangAndCodePage)) - 1 do
begin
if VerQueryValue(loc_InfoBuf,
PChar(Format('StringFileInfo\0x\FileVersion',
[PLangAndCodePageArray(lpTranslate)[i].wLanguage,
PLangAndCodePageArray(lpTranslate)[i].wCodePage])),
Pointer(loc_VerBuf), dword(loc_VerBufSize)) then
begin
result := loc_VerBuf;
Break;
end;
end;
finally
FreeMem(loc_InfoBuf, loc_InfoBufSize);
end;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
ShowMessage(GetFileVersion(Application.ExeName));
end;