PDA

View Full Version : renaming files on server


Paul Ferrie
01-16-2007, 12:57 PM
Hi guys,

I am having a bit of a problem renaming files on the server.

I have a form the uploads files and gives them a temp name.
56__thumb.jpg
56_0_image.jpg
56_1_image.jpg
56_2_image.jpg
56_3_image.jpg
56_4_image.jpg
And so on...
The second part of this form is to submit the data to the database and and use the returned mysql_insertid() to rename the files.

I cant get the file to rename.

Here is the code i am using.

if($result){
if ($handle = opendir("../media/gallery/")) {
$i=0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr_count($file, $tempID.'__thumb.jpg')==1){
rename("../media/gallery/".$tempID.'__thumb.jpg', "../media/gallery/".$moveID.'_thumb.jpg')
}else{
$i++;
}
}

}
closedir($handle);
}
}


Thanks for any help or input

Paul Ferrie
01-17-2007, 09:20 AM
I am now able to rename indevidual files

if ($handle = opendir("../media/gallery")) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if( substr_count($file, $tempID.'__thumb.jpg')==1){
@rename("../media/gallery/".$file, "../media/gallery/".$moveID.'_thumb.jpg');
@rename("../media/random/".$tempID."__random.jpg", "../media/random/".$moveID.'_random.jpg');
}
}
}
}


Still not able to rename multipule files though


$tempID=$_GET['tempID'];
$moveID=8;

if ($handle = opendir("../media/gallery/")) {
$i=0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//
if(substr_count($file, $tempID.'__thumb.jpg')==1){
echo "found=".$file."<br>";
}else if(substr_count($file, $tempID.'_'.$i.'_image.jpg')){
//rename("../media/gallery/".$file, "../media/gallery/".$moveID.'_'.$i.'_image.jpg');
echo "found=".$file;
}
echo $file."<br>";
echo substr_count($file, $tempID."_".$i."_image.jpg")."<br>";
$i++;
}
}
closedir($handle);
}



This returns 0 event though the echo below it shows the file :s


(substr_count($file, $tempID.'_'.$i.'_image.jpg'))


Anyone?
Please

Paul Ferrie
01-22-2007, 10:10 AM
Still stuck here renaming/moving files from folder A to folder B

Files are upload to a temp folder.

i then submit the form and run the script to move the files from the temp folder to the newly created folder.


$moveID = 1;

//^^ name of newly created folder
$tempPath = "../temp/";
$movePath = "../media/gallery/".$moveID."/";
if (!file_exists($movePath)) {
mkdir("../media/gallery/".$moveID,0777);
$movePath="../media/gallery/".$moveID;
chmod($movePath,0777);
}
$files = array();
if ($handle = opendir($tempPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($handle);
}
for($i = 0 ; $i < count($files) ; $i++) {
$file2 = $files[$i];
if($file2!='_thumb.jpg'){
rename($tempPath.$file2, $movePath.$i."_image.jpg");
echo "Image found =".$tempPath.$file2."<br>";
}else if($file2!=$i.'_image.jpg'){
rename($tempPath.$file2, $movePath."_thumb.jpg");
echo "Thumb found =".$tempPath.$file2."<br>";
}
}



Can anyone see anything wrong?
all looks correct to me but those damn files wont copy to the new folder.

Paul Ferrie
01-22-2007, 09:10 PM
Well finally got this sorted
Thougght i would post the cde so that other could make use of it.

$moveID = "1";
$tempPath = "../temp/";
$movePath = "../media/gallery/".$moveID."/";
if (!is_dir($movePath)) {
$newDir="/httpdocs/media/gallery/".$moveID;
$path="/httpdocs/media/gallery/";
function FtpMkdir($path, $newDir) {
$server='www.mydomain.com'; // ftp server
$connection = ftp_connect($server); // connection
$user = "user";
$pass = "pass";
$result = ftp_login($connection, $user, $pass);
if ((!$connection) || (!$result)) {
return false;
exit();
} else {
if (!ftp_chdir($connection, $path)) { // go to destination dir
$r = false;
} else if (!ftp_mkdir($connection,$newDir)) { // create directory
$r = false;
} else if (!ftp_site($connection, "CHMOD 0777 $newDir")){ // change attributes
$r = false;
} else {
$r = $newDir;
}
}
ftp_close($connection); // close connection
return $r;
}
$runMe=FtpMkdir($path,$newDir);
if(!$runMe){
echo "error";
}else{
$files = array();
if ($handle = opendir($tempPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($handle);
}
for($i = 0 ; $i < count($files) ; $i++) {
$file2 = $files[$i];
if($file2!='_thumb.jpg'){
copy($tempPath.$file2, $movePath.$i."_image.jpg");

print "Copying... <br>".$tempPath.$file2." to ". $movePath.$i."_image.jpg"."<br>";

}else if($file2!=$i.'_image.jpg'){
copy($tempPath.$file2, $movePath."_thumb.jpg");

print "Copying... <br>".$tempPath.$file2." to ".$movePath."_thumb.jpg" ."<br>";

}
}
}
}
?>