Log in

View Full Version : How to move a file?



sami
04-25-2007, 09:13 PM
I have an issue that’s been driving me crazy.
Here is how:

I used db to store the dir path and dir id.
dir_table
dir_id e.g. 1 (my first folder dir id)
dir_name e.g. folder1
dir_path e.g. localhost/upload/folder1

dir_id e.g. 2 (my second folder dir id)
dir_name e.g. folder2
dir_path e.g. localhost/upload/folder2

file_table
file_id e.g. 1 (my first folder dir id)
file_name e.g. file1
file_dir_id e.g. 1 (This first file belong to dir 1)
file_dir_path e.g. localhost/upload/folder1

I was able to call upon the db tables successfully. I see the file1 with a url that I can click to edit it. Once I click to edit the file1…I have the option to delete, edit, and move in the drop down menu. I selected file1 to move to folders2 from folder1. But somehow, the db only updates the file_dir_id from 1 to 2 and NOT file_dir_path.

Now I have this showing in my db file_table:
file_dir_id e.g. 2 (it is belong to dir 2 now)
file_dir_path e.g. localhost/upload/folder1 (file is still in folder1…should be folder2)

This is weird here. I was able to select the same file1 and move it back to folder1 from folder id 2 in the db but there’s no file1 in folder2 directory. It then moved the file1 from folder1 dir and place it in folder2 dir and the file_dir_path is now localhost/upload/folder1. when I tried to get the file again to move…there’s no more file but errors.

I used rename() to move my file.

If anyone understood what I am talking about, please help.

Thanks.

boxxertrumps
04-25-2007, 10:02 PM
Source please, it will help us in finding the problems and fixing them.

sami
04-26-2007, 01:27 PM
Here are the codes:


// move file
if(isset($_POST['submit']))
{
$imgcat = (!empty($_POST['imgcat'])) ? $_POST['imgcat'] : '';
if(!empty($imgcat))
{ //make sure it is from same dir
if($imgcat !== $row['img_category'])
{
// image and thumb paths
$oldfile = $row['img_path'] . "/" . $row['img_file'];
$newfile = $row['cat_path']. "/" .$row['img_file'];

$oldthumb = $row['img_path'] . "/" . $row['img_thumb'];
$newthumb = $row['cat_path'] . "/" .$row['img_thumb'];

$path = PATH_DIR . $row['cat_name'];

// move both image and thumb to dir
$dir = opendir($row['cat_path']);
readdir($dir);
rename($oldfile, $newfile);
closedir($dir);

$sql = "UPDATE " . IMAGES_TABLE . "
SET img_category = '$imgcat', img_path = '$path'
WHERE img_id = '$id'";
mysql_query($sql);

}else{
$display_msg = $lang['error_move_file'];
}
}else{
$display_msg = $lang['error_imgcat'];
}
}

Thanks,

boxxertrumps
04-26-2007, 08:56 PM
So the files aren't moved, and the DB isn't updated to reflect the move?
Is it in your SQL query to have the path updated?
(I'm sort of confused though, your example and the code don't match...)

sami
04-27-2007, 01:27 PM
Sorry...my explaination is bad. I think you kind of figured out what I am saying.

I tried this way to see if it works...and it does.




$dir = opendir("/images/old");
readdir($dir);

rename("/images/old/123.jpg", "/images/new/123.jpg");

closedir($dir);

$sql = "UPDATE images_table SET imgcat_id = '2', img_path = 'images/new'
WHERE img_id = '$id'";
mysql_query($sql) or die(mysql_error());




But this is not the way I want to do it. I want to move a that one file and have it update into the database the new category and the new path.

how would I achieve that?
thanks,