web view2. write a php code to complete programmer. copying, rename, write and append file....
TRANSCRIPT
2. Write a php code to complete programmer. Copying, Rename, Write and Append file.
a. copyfile.php
PHP Code:
<?php $soure = 'C:\xampp\php\install.txt'; $dest = 'C:\Users\amanoren\Desktop\install.php';
if (!copy($soure, $dest)) { echo "failed to copy $file...n"; }
else {
$msg= "Done Copy File and look at you file";}?><html><head><title>Copying files</title></head><body>
<?php echo ($msg);?></body></html>
In file C:\xampp\php\install.txt
AT MY DESKTOP
Before
Run The Copy Code With Xampp
AT MY DESKTOP
After
b. renamefile.php
PHP Code:
<?php
$old = 'C:\Users\amanoren\Desktop\install.php';$new = 'C:\Users\amanoren\Desktop\install123.php';
if(!rename($old, $new)){
echo "Unable to rename $old to $new...n";}else {
$msg= "Done Rename File and look at you Desktop";}?><html><head><title>Rename files</title></head><body>
<?php echo ($msg);?></body></html>
AT MY DESKTOP
Before
Run The Copy Code With Xampp
AT MY DESKTOP
After
c. writingfile.php
PHP Code :
<?php $filename = 'C:\Users\amanoren\Desktop\newFile.txt';$towrite = "Nama Saya Abdur Rahman Bin Nasaruddin";
$openedfile = fopen($filename, "w");fwrite($openedfile, $towrite);$whatsinthefile = file_get_contents($filename);echo ($whatsinthefile);?><html><head><title>Copying files</title></head><body>
<?php if(file_exists($filename)){
$file_length=filesize($filename);$msg= "<br>File created at $filename<br>";$msg.="containing $file_length bytes";echo $msg;
}else
echo "unble to create file";?>
</body></html>
AT MY DESKTOP
Before
Run The Copy Code With Xampp
AT MY DESKTOP
After
d. appendfile.php
PHP Code
<?php $filename = 'C:\Users\amanoren\Desktop\newFile.txt';$string = file_get_contents($filename);$string .=" Jabatan Teknologi Maklumat dan Komunikasi, Politeknik Ungku Omar, Jalan Raja Musa Mahadi, 31400, Ipoh";file_put_contents($filename, $string);$whatsinthefile = file_get_contents($filename);echo ($whatsinthefile);?><html><head><title>Append files</title></head><body><?php
if(file_exists($filename)){
$file_length=filesize($filename);$msg= "<br>File created at $filename<br>";$msg.="containing $file_length bytes";echo $msg;
}else
echo "unble to create file";?>
</body></html>
Run The Copy Code With Xampp
IN FILE newFile.txt
Before
After