Tag Archives: file
Making REST requests from PHP
The most simplest way is to use $data = json_decode( file_get_contents(“http://www.w3mentor.com/service.json”) ); This example won’t work on servers that explicitly disable the URL opening behavior, mostly seen in shared hosting environment.
Download file from server and manipulate it using C#
The following function expects a path whereby it checks for a file in the path supplied and downloads it to where the site is running from (server) for the system to manipulate the file however the user wants. using System; … Continue reading
Check for Acceptable File Types during upload in Perl
When a file with a Content-Type that isn’t text/html is uploaded to this CGI script, its output indicates that only HTML types are allowed. #!/usr/bin/perl use strict; use CGI qw/:standard/; my $q = new CGI; my $filename = … Continue reading
Monitoring the File System in c# .NET
The class that helps you to monitor the filesystem is the FileSystemWatcher class. It exposes several events that your application can catch. This enables your application to respond to file system events. The basic procedure for using the FileSystemWatcher is … Continue reading
Working with Comma-Separated Values in C# .NET
The example uses comma-separated values, loading them into a List object. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CommaValues { class Program { private static List<Dictionary<string, string>> GetData( out List<string> columns) { string line; string[] … Continue reading
Reading Data from Random Access Files
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ReadFile { class Program { static void Main(string[] args) { byte[] byData = new byte[200]; char[] charData = new Char[200]; try { FileStream aFile = new FileStream("sometext.txt", … Continue reading
Writing Data to Random Access Files
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace WriteFile { class Program { static void Main(string[] args) { byte[] byData; char[] charData; try { FileStream aFile = new FileStream("Temp.txt", FileMode.Create); charData = "Hello and welcome … Continue reading
How to upload multiple files using PHP and HTTP Post
We can implement this in two ways. One as an array of files and the other as individual variables. Method 1: In the html form, set multiple file input boxes, use the array name as their names, as follows: <form … Continue reading
Read file path from commandline and displays information about file and directory
The following example console application takes a file path from a commandline argument and then displays information about the file and the containing directory. using System; using System.IO; public class FileInform { private static void Main(string[] args) { … Continue reading
Cache thumbnail versions of an image and generate them if they do not exist
We can cache thumbnail versions of an image and generate them if they do not exist using .htaccess. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} -([0-9]+)x([0-9]+)\.(jpe?g|gif|png)$ [NC] RewriteRule ^(.*)-(.+)x(.+)\.(.{3,4})$ /thumb.php?file=$1.$4&width=$2&height=$3
htaccess flags to check File or Path
We can perform six tests to check a specific file or path exists using .htaccess. Given below are the flags used in htaccess when dealing with files and folders. -d Assumes the test string is a path name and the … Continue reading
using xcopy with PHP exec
The xcopy command can be used to move/copy files/folders from one place to another. Example: <?php exec(’xcopy c:\\myfolder d:\\myfolder /e/i’, $a, $a1); ?> by executing this command, it will move folder along with all contents to destination.
Retrieve or copy remote files using PHP
<?php if(!@copy(’http://someserver.com/somefile.zip’,’./somefile.zip’)) { $errors= error_get_last(); echo "COPY ERROR: ".$errors[’type’]; echo "<br />\n".$errors[’message’]; } else { echo "File copied from remote!"; } ?>
Copy files using shell_exec in PHP
This will create a directory called dir_dest. The permissions should be correct for this to work. <?php $output = shell_exec( " mkdir dir_dest; cp -r -a dir_source/* dir_dest 2>&1 " ) echo $output ?>
Copy files using stream in PHP using stream_copy_to_stream()
The stream_copy_to_stream() can be used to copy data from one stream to another. It makes a copy of up to maxlength bytes of data from the current position (or from the offset position, if specified) in source to dest. If … Continue reading
Recursively copy directories and subdirectories in PHP
<?php function recurse_copy($src,$dst) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != ‘.’ ) && ( $file != ‘..’ )) { if ( is_dir($src . ‘/’ . $file) ) { recurse_copy($src . … Continue reading
Create a copy of file without copy() in PHP
<?php function copyfiles($file1,$file2){ $contentx =@file_get_contents($file1); $openedfile = fopen($file2, "w"); fwrite($openedfile, $contentx); fclose($openedfile); if ($contentx === FALSE) { $status=false; }else $status=true; return $status; } ?>
Copy a file in PHP
The php copy() function can be used to copy a file. It makes a copy of the file source to dest. <?php $old = ‘C:\tmp\someold.txt’; $new = ‘C:\tmp\somenew.txt’; copy($old,$new) or die("couldn’t copy $old to $new"); ?>
Include files relative to current file in PHP
To include files relative to current file in PHP, the dirname() function is particularly useful in combination with the special constant __FILE__, which contains the full pathname of the current file. <?php $currentDir = dirname(__FILE__); include $currentDir . ‘/functions.php’; include … Continue reading