If you want to use PHP to create a file, the directory must be accessable and writable so the permissions must be 3 or 7. If you created the directory with the file manager the permissions would be 763 or 763 or 777. If you intend on using PHP to create or modify executable scripts, you want to be very careful about security. To start, you can hide the directory and give it a hard to guess name and do the same with the file that is doing the creating and modifying.
Files can be created with the touch() function. touch() is also used to set the access and modification times of a file. For more, check the PHP Manual. For now, we won't worry about changing the access and modification times. To create a file without specifying the modification time, just one argument is required, a file path.
touch("file_path");
If the file aready exists, its access time will be changed even if you do not specify it. If the file does not exist, it is created. The first form in Example 1 creates a file in the directory specified by $directory
Files can be delete with the unlink() function. The unlink() function has a single argument; the path to the file to be deleted:
unlink("file_path");
The second form in the Example 1 deletes files.
Finally a function with a name that doesn't need explanation. copy() has two arguments; a path to an existing file and a path to a new file:
copy("$old", "new.txt");
If a file with the same name as your new file already exists, it is overwritten without an error message being generated. If you don't want that, you can use file_exists() so prevent overwriting:
if (!file_exists("$old") )
{ copy("old.txt", "$new") }
else
{ die("That file alreay exists!"); }
rename() accepts two arguments; a path to an existing file and a path with a new name:
rename("$old", "$new");
If you attempt to rename a file with the name of an existing file, a warning is displayed, and the file is not renamed
<?
$directory = "directory_name";
# replace directory_name with your directory name
$filename = $_REQUEST[filename];
$kill = $_REQUEST[kill];
$copy_1 = $_REQUEST[copy_1];
$copy_2 = $_REQUEST[copy_2];
$rename_1 = $_REQUEST[rename_1];
$rename_2 = $_REQUEST[rename_2];
if ( isset($filename) )
{ touch("$directory/$filename"); }
if ( isset($kill) )
{ unlink("$directory/$kill"); }
if ( isset($copy_1) && isset($copy_2) )
{ copy("/$copy_1", "/$copy_2") }
if ( isset($rename_1) && isset($rename_2) )
{ copy("/$rename_1", "/$rename_2") }
?>
<br /><br />
<!-- Create a file form -->
<form>
Create File:
<input type="text" name="filename" />
<input type="submit" value="Create File" />
</form>
<br /><br />
<!-- Delete a file form -->
<form>
Delete File:
<input type="text" name="kill" /><br />
<input type="submit" value="Delete File" />
</form>
<br /><br />
<!-- Copy file form -->
<form>
Original name:
<input type="text" name="copy_1"><br />
Copy name
<input type="text" name="copy_2"><br />
<input type="submit" value="Copy File">
</form>
<br /><br />
<!-- Rename file form -->
<form>
Orig. name
<input type="text" name="rename_1"><br />
New name
<input type="text" name="rename_2"><br />
<input type="submit" value="Rename File">
</form>
In the section on creating images with imagepng(), imagegif() and imagejpeg(), I mention that they all can accept a file path as an optional argument. When you use that, you output the image to the named file instead of directly to the browser. When you do it this way, you don't need headers but you do need to have a file extension that matches what you output. (imagegif() outputs to a .gif file.)
<?
/* rename $directory to your directory, set to 777 */
$directory = "temp";
$img = $_REQUEST[img];
$x = $_REQUEST[x];
$y = $_REQUEST[y];
$r = $_REQUEST[r];
$g = $_REQUEST[g];
$b = $_REQUEST[b];
if ( isset($img) && isset($x) && isset($y) && isset($r) && isset($g) && isset($b) )
{
$image = imagecreate( $x, $y );
$color = imagecolorallocate( $image, $r, $g, $b);
imagegif($image, "$directory/$img.gif" );
}
?>
<html>
<body>
<form>
Gif file name (don't include extension):
<input type="text" name="img" /><br />
Width: <input type="text" cols="3" name="x" /> /
Height<input type="text" cols="3" name="y" /><br />
Red <input type="text" cols="2" name="r" /> /
Green <input type="text" cols="2" name="g" /> /
Blue <input type="text" cols="2" name="b" /> <br />
<input type="submit" />
</form>
</body>
</html>
|
|
|