Ok i have a problem i cant solve

I have 2 images

image one saved in location x and has name x1


image two is saved in location y and has name y1


Now i want a script that picks up image y1 and replaces x1 with y1 using changeing the name of the image to x1 as well.


this is what i tried so far but it has not work let me know if i am on the right track my php is rusty

// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

echo $newImg = file_get_contents('$directory_self/newImage/STC2033375M1.01.jpg');

echo "<br/>";


echo $fp = fopen('$directory_self/images/', 'w');

echo "<br/>";
echo fwrite($fp, $newImg, strlen($newImg));
fclose($fp);


echo "I ran the script the targetImage now looks like this <br/> <img src='images/targetImage.jpg'  width='300' />";

Dani AI

Generated

Good catch from — using PHP's filesystem functions is the cleanest approach — and 's confirmation that copy() worked on localhost matches the usual pattern. For a production-ready replace, prefer an atomic approach: validate paths and permissions, copy into the destination directory using a temporary filename, then rename the temp file to the final name (rename is atomic on the same filesystem). If rename fails (different mounts), fall back to a final copy and cleanup.

Example pattern (robust, memory‑efficient):

$src = $_SERVER['DOCUMENT_ROOT'] . '/newImage/source.jpg';
$dst = $_SERVER['DOCUMENT_ROOT'] . '/images/target.jpg';

if (!is_readable($src)) { error_log("Source not readable: $src"); exit; }
$dstDir = dirname($dst);
if (!is_dir($dstDir) || !is_writable($dstDir)) { error_log("Target dir not writable: $dstDir"); exit; }

$temp = $dstDir . '/.' . basename($dst) . '.tmp.' . uniqid();
if (!@copy($src, $temp)) { error_log("Copy to temp failed"); exit; }

if (!@rename($temp, $dst)) {
    // rename may fail across filesystems — try final copy then cleanup
    if (!@copy($temp, $dst)) { unlink($temp); error_log("Final replace failed"); exit; }
    unlink($temp);
}

Troubleshooting checklist:

  • Confirm absolute paths (use realpath or $_SERVER['DOCUMENT_ROOT']). Relative paths behave differently on CLI vs web.
  • Check ownership/permissions and open_basedir/SELinux restrictions; webserver user must be allowed to write the destination directory.
  • Avoid loading large images into memory (no need for file_get_contents/fwrite for whole files); copy() streams efficiently.
  • Use move_uploaded_file() for user uploads. rename() is fastest and atomic only when source and target are on the same filesystem; otherwise use copy+unlink or the temp+rename pattern above.
  • For concurrent updates, the temp+rename method reduces race conditions; consider file locks if multiple processes may write the same target.

Summary: copy() is simple and often sufficient (as seen in this thread), but temp+rename() gives safer, atomic replacement on production servers.

Recommended Answers

All 4 Replies

here is the link to my example that does not work

-- link removed (no longer available) --

I managed to this to work on my localhost so I guess its working now

Crazy man thanks

note: i did not need to use the rename function it just overwrites it using the same name that exists

this is amazing.

Thanks man I spent 3 days trying to find a solution to this and its so nice and clean

<?php
//$thisdir = getcwd(); // dont need this for localhost

// This is the file we want to transfer
$file = 'newImage/STC2033375M1.01.jpg';


// this is wher we want to store it
$newfile = 'images/TargetImage.jpg';





if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.