Trying to copy image file name to another

I am definitely a beginner. Here is what I am attempting to do. I am a ham (amateur) radio operator involved with other local ham radio operators that operate using a special event call sign at least twice a year. I have a website set up which the radio operators uploads to so other can see how they are doing. I would like to add the list of operators and show what band and mode they are operating on or that they are off the air. My thought is to have a series of images for each band and mode. Then have a web page for each operator in which they can select the band and mode. When they make their selection, I would like to copy the image file to another file name.

Below is one failed attempt to achieve what I wanted to do. The result always ends up being 80DIG.png being copied to VA3MSV.png no matter what I select. Any ideas would be appreciated

<title>VA3MSV Band Selction</title>
Hello John, VA3MSV.<br><br>What band are you currently on? Please select the band that you are currently operating on. When you go off the air please select "OFF THE AIR".<br><br>
<input type="button" onclick="<?php echo copy("OFFAIR.png","VA3MSV.png");?>" value="OFF THE AIR"></form><br>
<input type="button" onclick="<?php echo copy("160SSB.png","VA3MSV.png");?>" value="160 m SSB">
<input type="button" onclick="<?php echo copy("160CW.png","VA3MSV.png");?>" value="160 m CW ">
<input type="button" onclick="<?php echo copy("160DIG.png","VA3MSV.png");?>" value="160 m DIG">
<input type="button" onclick="<?php echo copy("80SSB.png","VA3MSV.png");?>" value=" 80 m SSB">
<input type="button" onclick="<?php echo copy("80CW.png","VA3MSV.png");?>" value=" 80 m CW ">
<input type="button" onclick="<?php echo copy("80DIG.png","VA3MSV.png");?>" value=" 80 m DIG"><br>

You need to create a function and use AJAX to accomplish this.

HTML and JavaScript:

<button onclick="copyFile('OFFAIR.png')">OFF THE AIR</button><br>
<button onclick="copyFile('160SSB.png')">160 m SSB</button>
<button onclick="copyFile('160CW.png')">160 m CW</button>
<!-- Add more buttons as needed -->

function copyFile(sourceFilename) {
    var xhr = new XMLHttpRequest();"POST", "copyfile.php", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("source=" + sourceFilename + "&destination=VA3MSV.png");

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {

PHP (copyfile.php):

if(isset($_POST['source']) && isset($_POST['destination'])) {
    $source = $_POST['source'];
    $destination = $_POST['destination'];
    if(copy($source, $destination)) {
        echo "File copied successfully.";
    } else {
        echo "Failed to copy file.";
1 Like

Php code is executed on the web server when the page is requested. All the php you have shown runs each time the page is requested, so that the last line, copying 80DIG.png to VA3MSV.png is the end result you see.

Back in the 1990s, when the WWW was first conceived, before server-side languages existed, every web page was statically created, requiring a lot of time to create and maintain. The main point of server-side languages is to get the computer to do the work for you. You should use a data-driven design to accomplish this, where you have a data structure (database table, array) where you define (insert/update/delete) the list of operators, then the code displaying the operators would simply loop over this defining data to produce the output.

To allow an operator to update their information, you need a login system to restrict access to only those that have permission to make changes. You would have a single edit/update page, but it would use the logged in user’s id to control which user’s data gets updated. The selection of the source file should use a post method form, with either a select/option menu or radio buttons (html, not the physical kind you use) and the destination file would be determined on the server based on the logged in user’s id.

You must validate all input data before using it. By unconditionally accepting file name(s) to operate on, anyone can copy any file to any other file on the server, either accidentally, intentionally, through a virus on a computer, or through cross site scripting, overwriting any file on the web server.

I could be wrong but I think ham operators have a set band that they use and doesn’t change that much, so they wouldn’t need it to be dynamically (though it would be more flexible).

Sponsor our Newsletter | Privacy Policy | Terms of Service