unlink 


php128 apg

DELETES a particular file.





Since PHP 5.0.0:

Context support was added.
Can be used with some URL wrappers.

A context is a set of parameters and wrapper specific options which modify or enhance the behavior of a stream.

Contexts are created using stream_context_create and can be passed to most filesystem related stream creation by functions like: fopen, file, file_get_contents, etc.

Options can be specified when calling stream_context_create, or later using stream_context_set_option.

See the link:

   Context options and parameters  !



Parameters can be specified for contexts using the stream_context_set_params function.

This function returns TRUE on success or FALSE on failure.



<?php

bool unlink 
( str $filename , res $context = ? )


where,

$filename = Path to the file

$context 
= The resource context as created by
                 stream_context_create 
or set by
                 stream_context_set_option
                       
?>

 $filename 


The path to the file to delete.



 $context 


A resource created by stream_context_create or set by stream_context_set_option.



  REGISTERED STREAM WRAPPERS   

STREAM CONTEXT WRAPPERS
ARRAY INDEXNAME
0php
1file
2glob
3data
4http
5ftp
6zip
7compress.zlib
8https
9ftps
10phar
ed48


  1 EXERCISE   

<?php 

define
("PAT1N", __DIR__ . '/temp/');

$srcpath01 = PAT1N; 

echo 
$srcpath01 . '<br><br>';

$srcfile01 = 'sw7.jpg'; 
// THIS FILE MUST EXIST

$dstfile01 = 'sw7.copy.jpg'; 

if (
file_exists($srcpath01 . $srcfile01)) 
{ 
if(
copy($srcpath01 . $srcfile01, $srcpath01 . $dstfile01)) 
{ 
echo 
'The ' . $srcfile01 . ' file WAS copied as ' . $dstfile01; 
} 
else 
{ 
echo 
'The ' . $srcfile01 . '  file WAS NOT copied as  ' . $dstfile01; 
} 
} 
else 
{ 
echo 
'The ' . $srcfile01 . ' file WAS NOT found.'; 
} 

?>  

  2 EXERCISE   

<?php 

define
("PAT2N", __DIR__ . '/temp');

$srcpath02 = PAT2N; 

echo 
$srcpath02 . '<br><br>';  

$srcfile02 = '/sw7.copy.jpg'; 

if (
file_exists($srcpath02 . $srcfile02)) 
{ 
if(
unlink($srcpath02 . $srcfile02)) 
{ 
echo 
'The ' . $srcfile02 . ' file WAS DELETED as expected.'; 
} 
else 
{ 
echo 
'The ' . $srcfile02 . ' file WAS NOT DELETED as expected.';
} 
} 
else 
{ 
echo 
'The ' . $srcfile02 . ' file WAS NOT found.'; 
} 

?>  

  3 EXERCISE   

<?php

define
("PAT3N", __DIR__ . '/temp'); 

echo 
PAT3N . '<br><br>';

$trg03 = PAT3N . '/t1/';

$lnk03 = 'symlk03a';

// CREATES the symlk03a
// REMEMBER to use this function on Windows
// you must run the server as Administrator

symlink($trg03, $lnk03);

sleep(10);

// This symlink must be removed 
// once its use has been completed
// in this case, after 10 seconds

// TRY TO REMOVE the symlk03a

unlink('symlk03a');

?>