Using rename

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fried
Forum Newbie
Posts: 16
Joined: Tue Sep 08, 2009 5:43 am

Using rename

Post 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"); 
        
        
        }
    }   
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Using rename

Post 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.
Post Reply