Jump to content

Simple Image Swap?


Go to solution Solved by xProteuSx,

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.