Lire les fichiers contenant du texte grâce au PHP est déjà facile. Il vous suffit d'utiliser le trio de fonctions fopen(), fgetc() et fclose().
Dans cet exemple, vous parcourez un fichier nommé "sample.txt" situé dans le même répertoire que le script. Et vous affichez chaque ligne à l'écran.
<?
$fichier = fopen("sample.txt","r") ;
while(!feof($fichier)) {
// On récupère une ligne
$Ligne = fgets($fichier,255);
// On affiche la ligne
echo $Ligne;
}
// On ferme le fichier
fclose($fichier);
?>
Tout ce code peut se résumer en une ligne :
<?
readfile("sample.txt") ;
?>
Maintenant, si vous voulez affecter tout le contenu du fichier à une variable, ne commencez pas fopen()... file_get_contents() suffit largement!
<?
// Affectation du contenu de sample à la variable $texte
$texte = file_get_contents("sample.txt") ;
?>
Vous désirez affecter chaque ligne dans une liste? Vous n'avez pas besoin d'incrémenter un compteur, et de compter les lignes ; file() le fait déjà .
<?
// Affectation du contenu de sample à la variable $texte
$texte = file("sample.txt") ;
?>
Tag Archive for file
Lire les fichiers textes
PHP Zip file creation, OOP
<?php
/*
By: Matt Ford
Purpose: Basic class to create zipfiles
*/
class zipFile {
public $files = array();
public $settings = NULL;
public $fileInfo = array (
"name" => "",
"numFiles" => 0,
"fullFilePath" => ""
);
private $fileHash = "";
private $zip = "";
public function __construct($settings) {
$this->zipFile($settings);
}
public function zipFile($settings) {
$this->zip = new ZipArchive();
$this->settings = new stdClass();
foreach ($settings as $k => $v) {
$this->settings->$k = $v;
}
}
public function create() {
$this->fileHash = md5(implode(",", $this->files));
$this->fileInfo["name"] = $this->fileHash . ".zip";
$this->fileInfo["numFiles"] = count($this->files);
$this->fileInfo["fullFilePath"] = $this->settings->path . "/" . $this->fileInfo["name"];
if (file_exists($this->fileInfo["fullFilePath"])) {
return array (
false,
"already created: " . $this->fileInfo["fullFilePath"]
);
}
else {
$this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE);
$this->addFiles();
$this->zip->close();
return array (
true,
"new file created: " . $this->fileInfo["fullFilePath"]
);
}
}
private function addFiles() {
foreach ($this->files as $k) {
$this->zip->addFile($k, basename($k));
}
}
}
$settings = array (
"path" => dirname(__FILE__)
);
$zipFile = new zipFile($settings);
$zipFile->files = array (
"./images/navoff.jpg",
"./images/navon.jpg"
);
list($success, $error) = $zipFile->create();
if ($success === true) {
//success
}
else {
//error because: $error
}
?>
programatically loading external js/css files
function loadExternalFiles(array) {
outString = "";
for (x in array) {
switch (array[x][1]) {
case "js":
outString += '<script type="text/javascript" src="' + array[x][0] + '"></script>';
break;
case "css":
outString += '<link rel="stylesheet" type="text/css" href="' + array[x][0] + '" />';
break;
}
}
return outString;
}
var serverName = "http://nric-20896/zWebHost/";
document.write(loadExternalFiles(new Array (
// [serverName + "js/fordmJS_misc.js", "js"], //combined with this file
[serverName + "skins/skin_0/css/mfstyle0.css", "css"],
[serverName + "skins/skin_0/css/layNav.css", "css"],
[serverName + "skins/skin_0/css/cal.css", "css"],
["http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js", "js"],
["http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/scriptaculous.js", "js"],
[serverName + "js/livepipe.js", "js"],
[serverName + "js/window.js", "js"],
[serverName + "js/calendar.js", "js"],
[serverName + "js/layNav.js", "js"]
)
));
ACCESS Open File with other program
'Open a File
Application.FollowHyperlink ("C:MyFile.doc")
'Open a Folder
Application.FollowHyperlink ("C:Program Files")
'Open a file where the file’s name and path is stored in a textbox on a form
Application.FollowHyperlink (me.YourTextBox)
File v. MD5 validator
<?php
if(md5_file($_GET['file'])==$_GET['md5']) { // Are the same
// Code
}
else { // Aren't the same
// Code
}
?>
Category: Uncategorized |
Tags: file, files, hash, md5, md5hash, security, validation, validator, validators
Loading Methods for External Files
package {
import flash.display.Sprite;
import flash.display.Loader;//Allows you to load an external file
import flash.net.URLRequest;//This is used to reference the external file
import flash.filters.DropShadowFilter;
public class LoadingMethods extends Sprite {
public function LoadingMethods(){
init();
}
private function init():void {
/**
Loading
*/
var loader:Loader = new Loader();
loader.load(new URLRequest("kitten.jpg"));
addChild(loader);
loader.x = 150;
loader.y = 150;
//For the loaded
var ds2:DropShadowFilter = new DropShadowFilter(10, 45, 0x00ff00,1,3,2);
var filterArray2:Array = [ds2];
loader.filters = filterArray2;
/**
Embedding
**/
//This is from importing the image into the lib and exporting for AS.
var myImage:Kitten = new Kitten();
addChild(myImage);
//For the embed
var ds:DropShadowFilter = new DropShadowFilter(10, 45, 0xff0000,1,3,2);
var filterArray:Array = [ds];
myImage.filters = filterArray;
}
}
}
Loop email reading flat file
<?php
set_time_limit(0); // this will keep the script from stopping if it takes longer then 30 seconds, must have this here
$emailaddress = file("email-list.txt"); // load from a flat file, assuming 1 email per line in the file
$emailsubject = "This is a sample subject!";
$emailbody = file_get_contents("email.html");
$fromaddress = "name@domain.tld";
$i = count($emailaddress);
$z = 0;
// here we check how many email address's we have, if its is 0, then we don't start the email function
if ($i != 0)
{// start if
// Lets loop until we reach the count from email address arrar
while ($i != $z)
{// start while
// here we send the email to the varables from above, using the email array incrament
mail($emailaddress[$z], $emailsubject, $emailbody, "From: " .$fromaddress. "
X-Mailer: PHP 4.x");
// lets echo out that the email was sent
echo $z + 1 . " out of " . $i . " emails sent. (" . $emailaddress[$z] . ")<br>";
// increment the array one, so we get a new email address from the array
++$z;
}// end while
}//end if
else
{//start else
// we echo out that no emails where found in the array and end the script
echo "Warning: No emails in array.";
}// end else
?>
Download file from URL
<?
// DOWNLOAD REMOTE FILE
// this is a-lot more complicated than it looks
// $url is the url of the file, must have fopen wrappers enabled
// $dir is the directory to save the file to relative to the current working directory
// file is saved with the same file name from its source
function getfile($url, $dir){
file_put_contents($dir.substr($url,strrpos($url,'/'),strlen($url)), file_get_contents($url));
}
?>
PHP ファイルãŒå˜åœ¨ã™ã‚‹ã‹ãƒã‚§ãƒƒã‚¯ã™ã‚‹
<?php
// ファイルãŒã‚ã‚‹ã‹ãƒã‚§ãƒƒã‚¯
$PATH = $_GET['path'];
if(file_exists($PATH)){
$file_exists = true;
}else{
$file_exists = false;
}
echo 'file_exists='.$file_exists;