-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomizefile.php
More file actions
67 lines (50 loc) · 1.28 KB
/
Copy pathrandomizefile.php
File metadata and controls
67 lines (50 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* [trouveNomUnique description]
* @param [type] $dirPath [description]
* @return [type] [description]
*/
function trouveNomUnique($dirPath) {
do {
$uniqueval=rand(1, 99999).".mp3";
$path = $dirPath . $uniqueval;
}
while (is_file($path));
return $uniqueval;
}
//verifie les arguments
if (!array_key_exists(1, $argv)) {
echo "usage : randomizefile.php repertoire";
exit();
}
$repsrc= $argv[1];
if (!is_dir($repsrc)) {
echo "Le répertoire ".$repsrc." n'existe pas.";
exit();
}
//tout est ok
if (substr($repsrc, -1)!=DIRECTORY_SEPARATOR) {
$repsrc=$repsrc.DIRECTORY_SEPARATOR;
}
$repsrc=str_replace("\\", "\\\\", $repsrc);
$repdst= $repsrc.'randomized'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR;
if (!is_dir($repdst)) {
mkdir($repdst);
}
array_map('unlink', glob($repdst.'*'));
$tabfile=glob($repsrc."*.mp3");
shuffle($tabfile);
foreach ($tabfile as $filename) {
$f=fopen($filename,'rb');
if ($f) {
$data=fread($f,filesize($filename));
fclose($f);
$randfile=trouveNomUnique($repdst);
//rewrite the file with the random name
$fd = fopen($repdst.$randfile, "wb");
fwrite($fd,$data,strlen($data));
fclose($fd);
echo $filename,'-->',$randfile, "\n";
}
}
?>