Tag Archive for files

File v. MD5 validator

<?php
if(md5_file($_GET['file'])==$_GET['md5']) { // Are the same
// Code
}
else { // Aren't the same
// Code
}
?>

source

Batch rename files with Perl regex substitutions

#!/usr/bin/env perl -w
use strict;

# Batch rename files with Perl regex substitutions
# Perl administration files rename
#
# Larry Wall's filename fixer: recipe 9.9 in Perl Cookbook

$op = shift
or die "Usage: $0 expr [files]
";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was, $_) unless $was eq $_;
}

source

File per Table

#!/bin/bash

if [ "$1" = "" ]; then
PASSWD=
else
PASSWD="-p$1"
fi

#
# DB_IGNORE_LIST is for those schemas which we do not want dump the data
#
DB_IGNORE_LIST="^information_schema$"
USER=root
SOCKET="/tmp/mysql.sock"

#
# Really shouldn't need to do much editing below here.
#
DB_LIST=`mysql -u root -S $SOCKET -e "SHOW DATABASES" -B --skip-column-names | egrep -v "$DB_IGNORE_LIST"`
for db in $DB_LIST
do
TABLE_LIST=`mysql -u root -S $SOCKET -B --skip-column-names -e "SHOW TABLES" $db`
for table in $TABLE_LIST
do
mkdir -p $db
mysqldump -S $SOCKET -u $USER $PASSWD $db $table> $db/$table.sql
done
done

source

PHP File extension case changer

<?php
$root = $_GET['root'];
$files = array_merge(glob("$root*.*"),glob("$root**.*"),glob("$root***.*"));
$z=0;
for($i=0;$i<count($files);$i++) {
if(stripos($files[$i],"Thumbs.db")) {
unlink($files[$i]);
$z++;
}
else {
rename($files[$i],strtolower($files[$i]));
}
}
echo "$i files renamed. $z Thumbs.db files deleted.";
?>

source

Scan and Require Ruby Files in Directory

class Dir
def self.require_all(directory)
self.entries(directory).each do |file|
if file =~ /.rb/
require directory + file
end
end
end
end

source

Recursively Remove Folder

/**
* Function used to delete a folder.
* @param $path full-path to folder
* @return bool result of deletion
*/
function folderDelete($path) {
if (is_dir($path)) {
if (version_compare(PHP_VERSION, '5.0.0') < 0) {
$entries = array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) $entries[] = $file;
closedir($handle);
}
}else{
$entries = scandir($path);
if ($entries === false) $entries = array();
}

foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
folderDelete($path.'/'.$entry);
}
}

return rmdir($path);
}else{
return unlink($path);
}
}

source

Show few lines from a file with sed

# show from line 12 to line 23
sed -n '12,23 p' archivo.txt

# or just one (line 15)
sed -n 15p archivo.txt

source

Unzip files

<?php
/**************************************
seesaw associates | <a href="http://seesaw.net" >http://seesaw.net</a>

client:
file:
description:

Copyright (C) 2008 Matt Kenefick(.com)
**************************************/

class zipArch {
/**
* Unzip the source_file in the destination dir
*
* @param   string      The path to the ZIP-file.
* @param   string      The path where the zipfile should be unpacked, if false the directory of the zip-file is used
* @param   boolean     Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
* @param   boolean     Overwrite existing files (true) or not (false)
*
* @return  boolean     Succesful or not
*/
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
if(function_exists("zip_open"))
{
if(!is_resource(zip_open($src_file)))
{
$src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
}

if (is_resource($zip = zip_open($src_file)))
{
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";

// Create the directories to the destination dir if they don't already exist
$this->create_dirs($dest_dir);

// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Now we're going to create the directories in the destination directories

// If the file is not in the root dir
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false)
{
// Create the directory where the zip-entry should be saved (with a "/" at the end)
$this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}

// Open the entry
if (zip_entry_open($zip,$zip_entry,"r"))
{

// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);

// Check if the files should be overwritten or not
if ($overwrite === true || $overwrite === false && !is_file($file_name))
{
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

if(!is_dir($file_name))
file_put_contents($file_name, $fstream );
// Set the rights
if(file_exists($file_name))
{
chmod($file_name, 0777);
echo "<span style="color:#1da319;">file saved: </span>".$file_name."<br />";
}
else
{
echo "<span style="color:red;">file not found: </span>".$file_name."<br />";
}
}

// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
}
else
{
echo "No Zip Archive Found.";
return false;
}

return true;
}
else
{
if(version_compare(phpversion(), "5.2.0", "<"))
$infoVersion="(use PHP 5.2.0 or later)";

echo "You need to install/enable the php_zip.dll extension $infoVersion";
}
}

function create_dirs($path)
{
if (!is_dir($path))
{
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);

foreach($directories as $directory)
{
$directory_path .= $directory."/";
if (!is_dir($directory_path))
{
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}

}
?>

source

php uploading files

//if they DID upload a file...
if($_FILES['photo']['name'])
{
//if no errors...
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops!  Your file's size is to large.';
}

//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations!  Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
}
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']

source

Download All Files from an Online Directory

wget --no-parent -ckr <a href="http://www.example.com/files

" >http://www.example.com/files
source