1--TEST--
2Test symlink(), linkinfo(), link() and is_link() functions : usage variations - access/update file through softlink
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6    die('skip no symlinks on Windows');
7}
8?>
9--FILE--
10<?php
11/* Prototype: bool symlink ( string $target, string $link );
12   Description: creates a symbolic link to the existing target with the specified name link
13
14   Prototype: bool is_link ( string $filename );
15   Description: Tells whether the given file is a symbolic link.
16
17   Prototype: bool link ( string $target, string $link );
18   Description: Create a hard link
19
20   Prototype: int linkinfo ( string $path );
21   Description: Gets information about a link
22*/
23
24/* Variation 3 : Create file and a soft link to the file
25                 Access data of the file through the soft link
26                 Update the file through soft link
27                 Check size of file and soft link link
28*/
29
30$file_path = dirname(__FILE__);
31echo "*** Accessing and updating data of file through soft link ***\n";
32// Creating file and inserting data into it
33$filename = "$file_path/symlink_link_linkinfo_is_link_variation3.tmp";
34
35// create temp file
36$file = fopen($filename, "w");
37
38// create soft link to file
39$linkname = "$file_path/symlink_link_linkinfo_is_link_link_variation3.tmp";
40var_dump( symlink($filename, $linkname) );
41// storing size of symlink in a local variable
42$link_stat = lstat($linkname);  // lstat of link
43$link_size = $link_stat[7];  // size of soft link
44
45// fill data into file
46fwrite($file, str_repeat("text", 20) );
47fclose($file);
48
49echo "\n-- Access data of the file through the soft link --\n";
50$data_from_link = file_get_contents($linkname);  // data read from $filename
51var_dump( $data_from_link );
52
53echo "\n-- Check size of soft link and file --\n";
54var_dump( filesize($filename) );
55var_dump( filesize($linkname) );
56
57// taking lstat of symlink
58$stat = lstat($linkname);
59// checking that size of symlink remains same
60if ($link_size == $stat[7])
61  echo "\nSoft link size remains same \n";
62else
63  echo "\nWarning: Soft link size has changed \n";
64
65echo "\n-- Updating file with data through soft link --\n";
66// append link with data
67$fp = fopen($linkname, "a");  // open in append mode
68fwrite($fp, "Hello World");
69fclose($fp);
70
71// now check temp file for data; it should append "Hello World"
72$data_from_file = file_get_contents($filename);
73var_dump( $data_from_file );
74
75echo "\n-- Check size of soft link and file --\n";
76var_dump( filesize($filename) );
77var_dump( filesize($linkname) );
78
79// taking lstat of symlink
80$stat = lstat($linkname);
81// checking that size of symlink remains same
82if ($link_size == $stat[7])
83  echo "\nSoft link size remains same \n";
84else
85  echo "\nWarning: Soft link size has changed \n";
86
87echo "\n-- Updating file with data and check data through soft link --\n";
88// write to temp file
89$file = fopen($filename, "w");
90fwrite($file, "Hello World");
91fclose($file);
92
93// now check link for data; it should echo "Hello World"
94$data_from_link = file_get_contents($linkname);
95var_dump( $data_from_link );
96
97echo "\n-- Check size of soft link and file --\n";
98var_dump( filesize($filename) );
99var_dump( filesize($linkname) );
100
101// taking lstat of symlink
102$stat = lstat($linkname);
103// checking that size of symlink remains same
104if ($link_size == $stat[7])
105  echo "\nSoft link size remains same \n";
106else
107  echo "\nWarning: Soft link size has changed \n";
108
109// delete the link
110unlink($linkname);
111// delete the temporary file
112unlink($filename);
113
114echo "Done\n";
115?>
116--EXPECTF--
117*** Accessing and updating data of file through soft link ***
118bool(true)
119
120-- Access data of the file through the soft link --
121string(80) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"
122
123-- Check size of soft link and file --
124int(80)
125int(80)
126
127Soft link size remains same
128
129-- Updating file with data through soft link --
130string(91) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttextHello World"
131
132-- Check size of soft link and file --
133int(91)
134int(91)
135
136Soft link size remains same
137
138-- Updating file with data and check data through soft link --
139string(11) "Hello World"
140
141-- Check size of soft link and file --
142int(11)
143int(11)
144
145Soft link size remains same
146Done
147