Tag Archive for file

Ignoring Hidden Files in AIR

private function iterateFiles(dir:File):void
{
if (!dir.isDirectory || dir.isHidden || !dir.exists || dir.name.search(/^..*$/) != -1) return;
var listing:Array = dir.getDirectoryListing();
for each(var f:File in listing)
{
if (this.fileList.length >= this.MAX_FILES) break;
if (f.isHidden || !f.exists || f.name.search(/^..*$/) != -1) continue;
if (f.isDirectory)
{
this.iterateFiles(f);
}
else
{
this.fileList.push(f);
}
}
}

source

Unzip file using PHP

<?php

// create object
$zip = new ZipArchive() ;

// open archive
if ($zip->open(’archive. zip’) !== TRUE) {
die (’Could not open archive’);
}

// extract contents to destination directory
$zip->extractTo(’/ tmp/extracted/ ‘);

// close archive
// print success message
$zip->close();
echo ‘Archive extracted to directory’;

?>

source

formatBytes

function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);

$bytes /= pow(1024, $pow);

return round($bytes, $precision) . ' ' . $units[$pow];
}

source

Grep for files that do not match a pattern

grep -L "the pattern" *

# to recurse into subdirectories

grep -RL "the pattern" *

source

comparing the checksums of two files with Perl and cksum

cksum file1 file2 | perl -ane '$x ||= $F[0]; warn if $x != $F[0]'

# This can be shortened slightly, to:

cksum file1 file2 | perl -ane 'warn if $F[0] != ($x ||= $F[0])'

# If the checksum value we need to match is already known, that can be
# shortened to:

cksum file1 file2 | perl -ane 'warn if "664253628" != $F[0]'

# on Mac, if you have copied a checksum value into the clipboard, use
# pbpaste to do the comparison

cksum file1 file2 | perl -ane 'warn if `pbpaste` != $F[0]'

source

Simple File Directory Listing Table

function listFiles($dir)
{
$output = '';  $outRows = '';  $files = array();
if (is_dir($dir)) {
if ($dirHandle = opendir($dir)) {
$files = array_diff(scandir($dir), array('.', '..', '.htaccess'));
$totalSize = (int) 0;
foreach($files as $file) {
$fileTime = @date("d-M-Y", filectime($dir . '/' . $file)) . ' ' . @date("h:i", filemtime($dir . '/' . $file));
$totalSize += filesize($dir . '/' . $file);
$fileSize = @byte_convert(filesize($dir . '/' . $file));
$cellLink = '<td class="list_files_table_file_link"><a href="/fake/files/' . $file . '">' . $file . '</a></td>';
$cellTime = '<td>' . $fileTime . '</td>';
$cellSize = '<td>' . $fileSize . '</td>';
$outRows .= '<tr>' . "
" . $cellLink . "
" . $cellTime . "
" . $cellSize . "
" . '</tr>' . "
";
}
closedir($dirHandle);
}
}
$output = '<table class="list_files_table" width="100%" align="center" cellpadding="3" cellspacing="1" border="0">' . "
";
$output .= '<thead><tr><td><b>Name</b></td><td><b>Last Modified</b></td><td><b>Size</b></td></tr></thead>' . "
";
$output .= '<tfoot><tr><td colspan="2">' . count($files) . ' files.</td><td>' . @byte_convert($totalSize) . '</td></tr></tfoot>' . "
";
$output .= '<tbody>' . "
";
$output .= $outRows;
$output .= '</body>' . "
";
$output .= '</table>';

return $output;
}

function byte_convert($bytes)
{
$symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$exp = (int) 0;
$converted_value = (int) 0;
if ($bytes > 0) {
$exp = floor(log($bytes)/log(1024));
$converted_value = ($bytes/pow(1024,floor($exp)));
}
return sprintf('%.2f ' . $symbol[$exp], $converted_value);
}

echo listFiles('/path/to/files');

source

Determine File Extension

// Use an XML object or an Array. This array is just an example
var _arrLinks:Array = ["testfiletype.txt", "http://www.domain.com/", "something.fdz", "another-file.rtf", "www.somedomain.com", "domain.com"];
var _fileExt:String;
var _strTmp:String;
for (var i:uint = 0; i < _arrLinks.length; i++)
{
_strTmp = _arrLinks[i];
_fileExt = _strTmp.substr(0, 4);

if (_fileExt == "www.") _fileExt = "www";
else if (_fileExt != "http") _fileExt = _strTmp.substr(_strTmp.lastIndexOf('.') + 1, _strTmp.length);

switch (_fileExt)
{
case "swf":
trace("This is a SWF file: " + _strTmp);
break;
case "txt":
trace("This is a TXT file: " + _strTmp);
break;
case "http":
trace("This is an HTTP link: " + _strTmp);
break;
case "www":
trace("This is a WWW link, convert to an HTTP link: <a href="http://"" >http://"</a> + _strTmp);
break;
case "com":
trace("This is a dot com link, convert to an HTTP link: <a href="http://"" >http://"</a> + _strTmp);
break;
default:
trace("Unrecognized link or file extension. Please add (" + _fileExt + ") to the switch statment or change your filename.");
break;
}
}

source

Make a Wav file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* M_PI is declared in math.h */
#define PI M_PI

typedef unsigned int	UI;
typedef unsigned long int	UL;
typedef unsigned short int	US;
typedef unsigned char	UC;
typedef signed int		SI;
typedef signed long int	SL;
typedef signed short int	SS;
typedef signed char	SC;

#define attr(a) __attribute__((a))

#define packed attr(packed)

/* WAV header, 44-byte total */
typedef struct{
UL riff	packed;
UL len	packed;
UL wave	packed;
UL fmt	packed;
UL flen	packed;
US one	packed;
US chan	packed;
UL hz	packed;
UL bpsec	packed;
US bpsmp	packed;
US bitpsmp	packed;
UL dat	packed;
UL dlen	packed;
}WAVHDR;

int savefile(const char*const s,const void*const m,const int ml){
FILE*f=fopen(s,"wb");
int ok=0;
if(f){
ok=fwrite(m,1,ml,f)==ml;
fclose(f);
}
return ok;
}

/* "converts" 4-char string to long int */
#define dw(a) (*(UL*)(a))

/* Makes 44-byte header for 8-bit WAV in memory
usage: wavhdr(pointer,sampleRate,dataLength) */

void wavhdr(void*m,UL hz,UL dlen){
WAVHDR*p=m;
p->riff=dw("RIFF");
p->len=dlen+44;
p->wave=dw("WAVE");
p->fmt=dw("fmt ");
p->flen=0x10;
p->one=1;
p->chan=1;
p->hz=hz;
p->bpsec=hz;
p->bpsmp=1;
p->bitpsmp=8;
p->dat=dw("data");
p->dlen=dlen;
}

/* returns 8-bit sample for a sine wave */
UC sinewave(UL rate,float freq,UC amp,UL z){
return sin(z*((PI*2/rate)*freq))*amp+128;
}

/* make arbitrary audio data here */
void makeaud(UC*p,const UL rate,UL z){
float freq=500;
UC amp=120;
while(z--){
*p++=sinewave(rate,freq,amp,z);
}
}

/* makes wav file */
void makewav(const UL rate,const UL dlen){
const UL mlen=dlen+44;
UC*const m=malloc(mlen);
if(m){
wavhdr(m,rate,dlen);
makeaud(m+44,rate,dlen);
savefile("out.wav",m,mlen);
}
}

int main(){
if(sizeof(WAVHDR)!=44)puts("bad struct");
makewav(22050,64000);
return 0;
}

source

FileUpload

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}else{
echo "Invalid file<br />";
echo $_FILES["file"]["type"]."<br />";
echo $_FILES["file"]["size"];
}
?>

source

Upload File Filter

*.aspx, *.ascx, *.asmx, *.htm, *.dll, *.rpt, *.js, *.jpg, *.jpeg, *.bmp, *.gif, *.css, *.png, *.exe, *.xls, *.doc, *.cab, *.doc

source