I've been through this code several times and still can't see what's wrong. This code is the contents of [url]/sig/index.php. Every time I run it, I get "Sorry there was an error". But when I check the values of the new and old files I'm trying to copy they're fine. Am I doing something stupid? I'm using the copy function in other pages on the same server so I'm sure it must be something wrong with the code.
This is a simple page to copy a file in the same directory, overwriting the previous file. I know the source files exist because I have a "Test link to source file" that always works.
<?php
$formval = $_POST["banners"] ;
$newname = "banner-main.png" ;
$dir = "/sig/" ;
$img_to_copy = $dir . $formval . ".png" ;
$newimg = $dir . $newname ;
if (copy($img_to_copy, $newimg)) {
echo ($formval . " is the new email signature image.") ;
} else {
echo "Sorry, there was an error.";
} ;
echo '<a href="' . $img_to_copy . '?var=' . rand(0,1000000) . '" target="_blank">Test link to source file</a>' ;
?>
<h1>Which banner is next?</h1>
<br>
<form action="/sig/index.php" method="post">
<input type="radio" name="banners" value="banner-1">Banner 1
<br>
<input type="radio" name="banners" value="banner-2">Banner 2
<br>
<input type="radio" name="banners" value="banner-3">Banner 3
<br>
<input type="radio" name="banners" value="banner-4">Banner 4
<br>
<input type="submit" value="Submit">
</form>
Most likely the cause of your problem is $dir = "/sig/"
you have there.
When viewing the file (in HTML) this uses the folder sig/
from the web server DocumentRoot of your domain/project.
When copying this will use the folder sig/
from your filesystem root (where the file doesn't exist).
Change $dir = "/sig/"
to $dir = "./sig/"
or use a full filesystem path when copying the file.