xProteuSx Posted April 1, 2013 Share Posted April 1, 2013 I have two files on a server, and I want to swap filenames like this: image1.jpg -> image2.jpg image2.jpg -> image1.jpg I cannot get this to work. Obviously I cannot do it this way because then I would end up with two copies of the same file, so I have tried this: $file1 = "image1.jpg"; $file2 = "image2.jpg"; $temp1 = "temp1.jpg"; $temp2 = "temp2.jpg"; rename($file1, $temp1); rename($file2, $temp2); rename($temp1, $file2); rename($temp2, $file1); Also, I have tried this: $file1 = "image1.jpg"; $file2 = "image2.jpg"; $temp1 = "temp1.jpg"; $temp2 = "temp2.jpg"; copy($file1, $temp1); copy($file2, $temp2); unlink($file1); unlink($file2); rename($temp1, $file2) rename($temp2, $file1); I have spent about 3 hours on this seemingly simple code Please have mercy on me! Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/ Share on other sites More sharing options...
Jessica Posted April 2, 2013 Share Posted April 2, 2013 Rename($file1, 'temp'); Rename($file2, 'file1); Rename($file1, 'file2'); Pseudo code. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422358 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 I think you meant: Rename($file1, 'temp');Rename($file2, '$file1);Rename($temp , '$file1'); ... its been tried Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422360 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 oops ... Rename($file1, 'temp');Rename($file2, '$file1);Rename($temp , '$file2'); Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422361 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 Doesn't work for some reason. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422362 Share on other sites More sharing options...
Jessica Posted April 2, 2013 Share Posted April 2, 2013 Post your actual code. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422363 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 Variables: $img_thumb1 = $nfilename1 = "Venezuela_p79_1000_Bolivares_1998_t.jpg"; $img_thumb2 = $nfilename2 = "Venezuela_p82_20000_Bolivares_1998_t.jpg"; $temp1 = "../upload/283/temp1.jpg"; $temp2 = "../upload/283/temp2.jpg"; $file1 = "../upload/283/" . $img_thumb1; $file2 = "../upload/283/" . $img_thumb2; Different things that I have tried: rename($file1, $temp1); rename($file2, $file1); rename($temp1, $file1); ... and ... copy($file1, $temp1); copy($file2, $temp2); unlink($file1); unlink($file2); rename($temp1, $nfilename2); rename($temp2, $nfilename1); ... and ... rename ($file1, $temp1); rename ($file2, $temp2); rename($temp1, $nfilename2); rename($temp2, $nfilename1); I wonder if this has something to do with 'script speed vs renaming speed' or 'script speed vs copying speed'. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422364 Share on other sites More sharing options...
Jessica Posted April 2, 2013 Share Posted April 2, 2013 rename($file1, $temp1); rename($file2, $file1); rename($temp1, $file2); Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422365 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 Yeah, sorry, I had this: rename($file1, $temp1);rename($file2, $file1);rename($temp1, $file2); (I just gave you some other code). This code only does the first half of the job. In the end both thumbs end up being the same. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422366 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 Also tried this: copy($file1, $temp1); copy($file2, $temp2); copy($temp1, $file2); copy($temp2, $file1); unset($temp1); unset($temp2); Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422367 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 OK, this seems to work (just needed to refresh the browser): copy($file1, $temp1); copy($file2, $temp2); copy($temp1, $file2); copy($temp2, $file1); unset($temp1); unset($temp2); Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422368 Share on other sites More sharing options...
Jessica Posted April 2, 2013 Share Posted April 2, 2013 If you want to take twice as many steps, sure. Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422371 Share on other sites More sharing options...
xProteuSx Posted April 2, 2013 Author Share Posted April 2, 2013 Seems like all the code was working ... it was just an issue with refreshing page so that it overwrote the cached files. *sheepish grin* Link to comment https://forums.phpfreaks.com/topic/276402-simple-image-swap/#findComment-1422375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.