filesystem.php ➔ BaseName1()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 7
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package		fwolflib
4
 * @subpackage	func
5
 * @copyright	Copyright 2006-2010, Fwolf
6
 * @author		Fwolf <[email protected]>
7
 * @since		2006-10-07
8
 */
9
10
11
require_once(dirname(__FILE__) . '/../fwolflib.php');
12
13
14
/**
15
 * Manual get basename instead of using pathinfo()
16
 *
17
 * @deprecated      Use native basename()
18
 * @param	string	$filename
19
 * @return	string
20
 */
21 View Code Duplication
function BaseName1 ($filename) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
	$i = strrpos($filename, '/');
23
	if (false === $i)
24
		return $filename;
25
	else
26
		return substr($filename, $i + 1);
27
} // end of func BaseName1
28
29
30
/**
31
 * Delete a dir or file completedly
32
 * 	When del a dir, del all dir and files under it also.
33
 *
34
 * @deprecated      Use Fwlib\Util\FileSystem::del()
35
 * @param	string	$name
36
 */
37
function DelFile ($name) {
38
	//:Notice: Lost link file will got nothing using realpath, basename, dirname
39
	//	So trans in full path as $name all the time.
40
	if(!is_link($name))
41
		$name = realpath($name);
42
	if (is_dir($name) && !is_link($name))
43
	{
44 View Code Duplication
		foreach (scandir($name) as $file)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
		{
46
			if (('.' == $file) || ('..' == $file))
47
				continue;
48
			DelFile($name . '/' . $file);
0 ignored issues
show
Deprecated Code introduced by
The function DelFile() has been deprecated with message: Use Fwlib\Util\FileSystem::del()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
49
		}
50
		rmdir($name);
51
	}
52
	else
53
	{
54
		//echo "unlink($name)\n";
55
		//:Notice: Only del trash files is allowed.
56
		if (0 < stripos($name, 'trash'))
57
			unlink($name);
58
		else
59
			echo "Error!!! No trash files included!\n";
60
	}
61
} // end of func DelFile
62
63
64
/**
65
 * Manual get dirname instead of using pathinfo()
66
 * Result didn't include ending '/'
67
 *
68
 * @deprecated      Use native dirname()
69
 * @param	string	$filename
70
 * @return	string
71
 */
72 View Code Duplication
function DirName1 ($filename) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
	$i = strrpos($filename, '/');
74
	if (false === $i)
75
		return '';
76
	else
77
		return substr($filename, 0, $i);
78
} // end of func DirName1
79
80
81
/**
82
 * Count size of a directory, recursive
83
 * 	 This func also recursive executed automatic
84
 *
85
 * @deprecated      Use Fwlib\Util\FileSystem::getDirSize()
86
 * @param	string	$path
87
 * @return	long
88
 */
89
function DirSize ($path) {
90
	$i = 0;
91
	$files = scandir($path);
92
	foreach ($files as $file)
93
	{
94
		if (('.' != $file) && ('..' != $file))
95
		{
96
			$pathfull = $path . '/' . $file;
97
			if (is_dir($pathfull))
98
			{
99
				$i += DirSize($pathfull);
0 ignored issues
show
Deprecated Code introduced by
The function DirSize() has been deprecated with message: Use Fwlib\Util\FileSystem::getDirSize()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
100
			}
101
			else
102
				$i += FileSize1($pathfull);
0 ignored issues
show
Deprecated Code introduced by
The function FileSize1() has been deprecated with message: Use Fwlib\Util\FileSystem::getFileSize()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
103
		}
104
	}
105
	return $i;
106
}// end of func DirSize
107
108
109
/**
110
 * Manual get extension instead of using pathinfo()
111
 *
112
 * @deprecated      Use Fwlib\Util\FileSystem::getFileExt()
113
 * @param	string	$filename
114
 * @return	string
115
 */
116
function FileExt1 ($filename) {
117
	$i1 = strrpos($filename, '.');
118
	$i2 = strrpos($filename, '/');
119
	if ($i1 < $i2)
120
		return '';
121
	else
122
		return substr($filename, $i1 +1);
123
} // end of func FileExt1
124
125
126
/**
127
 * Manual get filename instead of using pathinfo()
128
 *
129
 * @deprecated      Use Fwlib\Util\FileSystem::getFileName()
130
 * @param	string	$filename
131
 * @return	string
132
 */
133 View Code Duplication
function FileName1 ($filename) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
	$basename = Basename1($filename);
0 ignored issues
show
Deprecated Code introduced by
The function BaseName1() has been deprecated with message: Use native basename()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
135
	$i = strrpos($basename, '.');
136
	if (false === $i)
137
		return $basename;
138
	else
139
		return substr($basename, 0, $i);
140
} // end of func FileName1
141
142
143
/**
144
 * Count size of a file
145
 * 	 This func is diffrence to php func filesize,
146
 *		It use stat & count blksize & blocks file used.
147
 *		11 = blksize, blocksize of filesystem IO *
148
 *		12 = blocks, number of blocks allocated
149
 *
150
 * @deprecated      Use Fwlib\Util\FileSystem::getFileSize()
151
 * @param	string	$file
152
 * @return	long
153
 */
154
function FileSize1 ($file) {
155
	if (is_link($file))
156
		$s = lstat($file);
157
	else
158
		$s = stat($file);
159
	if (-1 == $s['blksize'])
160
		$size = $s['size'];
161
	else
162
		$size = $s['blksize'] * $s['blocks'] / 8;
163
	return($size);
164
} // end of func FileSize1
165
166
167
/**
168
 * Get/gen a filename to write as a new file.
169
 *
170
 * Before write, there is a filename, but if file exists,
171
 * need plus -1, -2, -nnn at end of filename but before file extention,
172
 * this func will do this job and return suitable filename.
173
 *
174
 * Will remove special chars in filename.
175
 *
176
 * Can use with dir as well as regular file.
177
 *
178
 * @deprecated      Use Fwlib\Util\FileSystem::getNewFile()
179
 * @param	string	$s_file	Path to dest file.
180
 * @return	string
181
 */
182
function GetFilenameToWrite ($s_file) {
183
	// Remove special chars in filename
184
	$ar = array('?', '&', ';', '=', ':', "\\");
185
	$s_file = str_replace($ar, '_', $s_file);
186
	$s_file = trim($s_file);
187
188
	$s_dir = DirName1($s_file) . '/';
0 ignored issues
show
Deprecated Code introduced by
The function DirName1() has been deprecated with message: Use native dirname()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
189
	$s_name = FileName1($s_file);
0 ignored issues
show
Deprecated Code introduced by
The function FileName1() has been deprecated with message: Use Fwlib\Util\FileSystem::getFileName()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
190
	$s_ext = FileExt1($s_file);
0 ignored issues
show
Deprecated Code introduced by
The function FileExt1() has been deprecated with message: Use Fwlib\Util\FileSystem::getFileExt()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
191
192
	// Auto skip exists file, no overwrite.(-1, -2...-9, -10, -11.ext)
193
	$i = 1;
194
	while (file_exists($s_file)) {
195
		$s_file = $s_dir . $s_name;
196
		$s_file .= '-' . strval($i++);
197
		if (!empty($s_ext))
198
			$s_file .= '.' . $s_ext;
199
	}
200
201
	return $s_file;
202
} // end of func GetFilenameToWrite
203
204
205
/**
206
 * List files and file-information of a directory
207
 * 	By default, sort files by mtime asc
208
 *  Returned array is started from 1
209
 *
210
 * @deprecated      Use Fwlib\Util\FileSystem::listDir()
211
 * @param	string	$dir
212
 * @return	array
213
 */
214
function ListDir ($dir) {
215
	//List files
216
	$dir = realpath($dir);
217
	if (empty($dir))
218
		return(false);
219
	if (!is_dir($dir))
220
		return(false);
221
	$dirfiles = scandir($dir);
222
	if (empty($dirfiles))
223
		return(false);
224
225
	$filename=array();
226
	$filemtime=array();
227
	// Parallel arrays (ignore the ".", "..")
228
	foreach ($dirfiles as $s)
229
	{
230
		if (('.' != $s) && ('..' != $s))
231
		{
232
			$filename[] = $s;
233
			$filemtime[] = filemtime($dir . '/' . $s);
234
		}
235
	}
236
	// Gen array
237
	// Mtime maybe same, so index by name temporary
238
	$ar_t = array();
239
	foreach ($filename as $k => $v) {
240
		$ar_t[$v] = $filemtime[$k];
241
	}
242
	// Sort by mtime
243
	asort($ar_t, SORT_NUMERIC);
244
	// Build result array, count file or dir size
245
	$i = 0;
246
	$files = array();
247
	foreach ($ar_t as $key=>$value)
248
	{
249
		$i++;
250
		$files[$i]['name'] = $key;
251
		$files[$i]['mtime'] = $value;
252
		//file or dir's size
253
		//If not use pathfull, same-named file and '..' will be so bad
254
		$pathfull = $dir . '/' . $key;
255
		if (is_link($pathfull))
256
		{
257
			$files[$i]['size'] = FileSize1($pathfull);
0 ignored issues
show
Deprecated Code introduced by
The function FileSize1() has been deprecated with message: Use Fwlib\Util\FileSystem::getFileSize()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
258
		}
259
		elseif (is_dir($pathfull))
260
		{
261
			$files[$i]['size'] = DirSize($pathfull);
0 ignored issues
show
Deprecated Code introduced by
The function DirSize() has been deprecated with message: Use Fwlib\Util\FileSystem::getDirSize()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
262
		}
263
		else
264
		{
265
			$files[$i]['size'] = FileSize1($pathfull);
0 ignored issues
show
Deprecated Code introduced by
The function FileSize1() has been deprecated with message: Use Fwlib\Util\FileSystem::getFileSize()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
266
		}
267
	}
268
	// Some maintainence
269
	unset($dirfiles, $filename, $filemtime, $ar_t);
270
	return($files);
271
} // end of func ListDir
272
273
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
274