Tag Archive for file

AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

Cross domain file check

$imgfile="<a href="http://www.crossdomain.com/image.jpg";" >http://www.crossdomain.com/image.jpg";</a>
try
{
	$k=@file_get_contents($imgfile);

} catch (Exception $e) {
	$k=false;
}

if($k!=false)
{
	$result .= "<img src='$imgfile'>";
	
}
else
{
	$result.="no image file";
}

source

Read arithmetic binary data using fstreams #2

unsigned short fread_unsigned_short(ifstream &input_file) { //note that the ifstream is passed by reference
    unsigned short num;
    //the cast reinterpretation is very importnant
    input_file.read(reinterpret_cast < char * > (&num),sizeof(unsigned short));
    return num;
}

ifstream input_data;
input_data.open("input.dat",ios::in | ios::binary); //note the modifiers that denote both input file and binary mode
unsigned short num=fread_unsigned_short(input_data);

/*
The above function can be generalized to any numeric datatype like integers, floats e.t.c.
just replace the type 'unsigned short' with the wanted type
*/

source

Retrieve the physical location of a dynamic dataset, file or folder in SAS

data mydata.class;
set sashelp.class;
run;
 
libname mydat6 v6 "%sysfunc(pathname(mydata))";
 
data mydat6.class6;
set mydata.class;
run;

source

Howto list all the file extension types in an SVN log dump

svn log -v > myrepo.log

perl -ne 'print qq{$1
} if m{^s+.*(.[^.s]w+)$}' myrepo.log | sort | uniq -c | sort -r

source

VB.NET Log File

Sub Log(ByVal txt As String)
        'Open a file for writing
        Dim FILENAME As String = Server.MapPath("Log.txt")

        'Get a StreamReader class that can be used to read the file
        Dim objStreamWriter As StreamWriter
        
        objStreamWriter = File.AppendText(FILENAME)

        objStreamWriter.WriteLine(DateTime.Now.ToString() + " >> " + txt)
    
        'Close the stream
        objStreamWriter.Close()
    End Sub

source

ファイル名を日本語に変換

mb_convert_encoding($file, "EUC-JP", "SJIS, UTF-8, JIS, ASCII")

source

フォルダ内のファイル一覧

function scanDirectory($node, $limit=0, $separator=""){
	//validate:
	if(!is_dir($node)){return null;}; $newSeparator=($separator=="")?"/":$separator;
	$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
	$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
	substr($node, 0, strlen($node)-1):$node;
	//initialize:
	$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
	$stack=array( array($node, $slashes) ); $directories=array(); $files=array(); $output=array();
	//RUN:
		while(sizeof($stack) && $stack[0][1]!=$limit){
		$current=array_shift($stack);
		$items=opendir($current[0]);
			while( ($it=readdir($items))!==false ){
				if( strrpos($it,".") === (strlen($it)-1) ){continue;};
				$path=$current[0].$newSeparator.$it;
				$output[]=$path;
				if(is_dir($path)){
					$files = dirList($path);
					$directories[$path]=$files; 
					$stack[]=array( $path, substr_count($path, $newSeparator) );
				};
			};
		}

	return $directories;
	/* keep this comment to reuse freely:*/
}

function dirList ($directory) 
{

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = mb_convert_encoding($file, "EUC-JP", "SJIS, UTF-8, JIS, ASCII");
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

$a = scanDirectory($path);

source

Log4r: how to use RollingFileOutputter: automatically cycle stale logfiles

# How to use RollingFileOutputter 

$: << "../src"

require 'log4r'

include Log4r 

puts "this will take a while" 

# example of log file being split by time constraint 'maxtime'

config = {

  "filename" => "logs/TestTime.log",

  "maxtime" => 10,

  "trunc" => true

}

timeLog = Logger.new 'WbExplorer'

timeLog.outputters = RollingFileOutputter.new("WbExplorer", config)

timeLog.level = DEBUG 

# log something once a second for 100 seconds

100.times { |t|

  timeLog.info "blah #{t}"

  sleep(1.0)

} 

# example of log file being split by space constraint 'maxsize'

config = {

  "filename" => "logs/TestSize.log",

  "maxsize" => 16000,

  "trunc" => true

}

sizeLog = Logger.new 'WbExplorer'

sizeLog.outputters = RollingFileOutputter.new("WbExplorer", config)

sizeLog.level = DEBUG 

# log a large number of times

100000.times { |t|

  sizeLog.info "blah #{t}"

} 

puts "done! check the two sets of log files in logs/ (TestTime and TestSize)"

source

Serve PDF file with PHP

// pdfString is the *contents* of a file.
// to serve a file that exists on the file system, use readfile() instead of print $pdfString

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($pdfString));
header('Content-Disposition: attachment; filename=' . 'pdf_dynamic.pdf');
ob_clean();
flush();
print $pdfString;
exit;

source