Page 1 of 1

Using rename

Posted: Thu Oct 22, 2009 12:37 am
by fried
I'm having difficulty trying to find the correct sequence to do this. There is a lot of conflicting information on the internet and you guys have been on the nail so far.

I'm adding functionality to a image gallery that lets me move images to different themes, I have previously added the functionality to add different themes, so once a theme is created this info is stored in the database. I now need to be able to move the image files to the correct location. I'm using rename but I get the die error. Maybe I should create the dir when I add the theme. Not sure if it's connected but if I move a file from a theme that's image dir exists to a new theme that doesn't, then return it to it's original theme it's image disappears. Any pointers to a good tutorial would be useful, as well as specific advice.

Thanks in advance.

Code: Select all

if ($cmd=="movei"){ 
    
    
        if(isset($_POST)){
        
        $visid= htmlentities($_POST['visid']);
        $atheme= htmlentities($_POST['atheme']); 
        
        $query = "SELECT theme, fichier FROM visuel WHERE idvis= '".$visid."'";
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_row($result);
        $newdir='../'.$row[0].'';
        mkdir($newdir);
        $oldfilename='../'.$row[0].'/'.$row[1].'';
        
        $query = "UPDATE visuel SET theme= '".$atheme."' WHERE idVis= '".$visid."'";
        @mysql_query($query);
        
        $query = "SELECT theme, fichier FROM visuel WHERE idvis= '".$visid."'";
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_row($result);
        
        
        $newfilename='../'.$row[0].'/'.$row[1].'';
        
        echo $oldfilename;
        echo $newfilename;
        rename ($oldfilename, $newfilename) or die ("Could not rename 
  directory"); 
        
        
        }
    }   

Re: Using rename

Posted: Thu Oct 22, 2009 1:30 am
by cpetercarter
This could be a permissions problem. If you "rename" a file to a directory which does not yet exist, php can create the new directory only if it has write permissions in the parent directory of the new directory.