OK, here’s the background…
I have a problem with a PHP script which moves files from one directory to another directory. I didn’t write the PHP script, and I’m not extremely fluent in PHP.
The background is that a directory will contain files in the format of:
MSG???.???
For example:
msg0000.wav
msg0000.txt
msg0000.GSM
msg0001.wav
etc.
The script works to move files like that. The problem is that I incorporated another program which creates an RSS feed from the sound files, and so another file is created which would have a format similar to
msg0000.7025f35d463ebbafa101db8a88c71b681aa8443d.mp3
msg0001.9a2755d835707fbfa72f41005d378e3d488c991b.mp3
Once those files are out there, when the PHP script is invoked, the moving of files between folders will end up in one file getting moved and the rest getting deleted or overwritten because the algorithm that does the moving doesn’t account for the long file name with the msg??? prefix. Even the wav and txt files are affected. If I move files from one folder to a destination folder using the script, the destination folder will end up with files that have the following format:
msg00.b00a00e00da00d00c00b00f00c00b00ca00.mp00
msg00.b00ae00ed00cfa00fcf00a00f00a00b00b00b00f00.mp00
msg00.gsm
msg00.txt
msg00.WAV
msg00.wav
I believe since all the files are being copied over as msg00 the files are overwriting each other when they are copied. Since they get deleted from the source directory, the files are unrecoverable.
The applicable code is below. Any thoughts on what it is I need to change? Thanks in advance.
function moveVoicemailData($files,$context_rx,$extension_rx,$folder_rx)
{
global $ASTERISK_VOICEMAIL_PATH;
$perm = fileperms($ASTERISK_VOICEMAIL_PATH);
$uid = fileowner($ASTERISK_VOICEMAIL_PATH);
$gid = filegroup($ASTERISK_VOICEMAIL_PATH);
// recieving path
$paths = split(';',$ASTERISK_VOICEMAIL_PATH);
$path_rx = appendPath($paths[0],$context_rx);
if (!is_dir($path_rx)) {
mkdir($path_rx, $perm);
chown($path_rx,intval($uid));
chgrp($path_rx,intval($gid));
}
$path_rx = appendPath($path_rx,$extension_rx);
if (!is_dir($path_rx)) {
mkdir($path_rx, $perm);
chown($path_rx,intval($uid));
chgrp($path_rx,intval($gid));
}
$path_rx = appendPath($path_rx,$folder_rx);
if (!is_dir($path_rx)) {
mkdir($path_rx, $perm);
chown($path_rx,intval($uid));
chgrp($path_rx,intval($gid));
}
// get recieving folder last message number
if (is_dir($path_rx)) {
$lastNum = -1;
$lastNumLen = 4;
$dh = opendir($path_rx);
while (false != ($filename = readdir($dh))) {
if($filename!="." && $filename!="..") {
$msg_path = $path_rx;
$msg_path = appendPath($msg_path,$filename);
if (is_file($msg_path)) {
$path_parts = pathinfo($msg_path);
$num = preg_replace("/[a-zA-Z]|\./",'', $path_parts['basename']);
if ($num > $lastNum) {
$lastNum = $num;
$lastNumLen = strlen($lastNum);
}
}
}
}
}
else {
$_SESSION['ari_error'] = sprintf(_("Could not create folder %s on the server"),$folder_rx);
return;
}
// copy files to new location, incrementing each message number
asort($files);
foreach($files as $key => $path) {
// get file parts for search
$path_parts = pathinfo($path);
$path = $path_parts['dirname'];
$path = fixPathSlash($path);
list($name,$ext) = split("\.",$path_parts['basename']);
if (is_dir($path)) {
$lastNum++;
$hdl = opendir($path);
while ($fn = readdir($hdl)) {
if (preg_match("/" . $name . "/",$fn)) {
$src = $path . $fn;
$path_parts = pathinfo($src);
$folder_rx = preg_replace("/\d+/",sprintf("%0" . $lastNumLen .
"d",$lastNum),$path_parts['basename']);
$dst = appendPath($path_rx,$folder_rx);
if (is_writable($src) && is_writable($path_rx)) {
$perm = fileperms($src);
$uid = fileowner($src);
$gid = filegroup($src);
copy($src,$dst);
if (is_writable($dst)) {
chmod($dst, $perm);
chown($dst,intval($uid));
chgrp($dst,intval($gid));
}
unlink($src);
}
else {
$_SESSION['ari_error'] = sprintf(_("Permission denied on folder %s or %s"),$src,$path_rx);
return;
}
}
}
closedir($hdl);
}
}
}
/*