简体   繁体   中英

How can I copy/rename/overwrite image using PHP, submit and radio buttons?

I am just starting out with PHP, and I am trying to make a way for a client to be able to change an image using radio buttons and form submission. My intention is for one of 2 image files (Open/Closed.jpg) to be copied and renamed to another location (images/status/Status.jpg) so that coding isn't needed to make the change.

The "new" image will then be used elsewhere. I have tried various copy() and rename() calls, but with no luck. Below is the code I am working with. I feel my problem lies with the submit call, but all things I have tried have not worked. So I am back to my original code (below) trying for a fresher start with help from seasoned users/programmers.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>

<body>

<?php 
// define variables and set to empty values
$WinterStatusErr = "";
$WinterStatus = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["WinterStatus"]))
     {$WinterStatusErr = "Status is required";}
   else
     {$WinterStatus = test_input($_POST["WinterStatus"]);}
}
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<p><span class="error"><font color="red"><b>* required selection</b></font></span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   <input type="radio" name="WinterStatus" <?php if (isset($WinterStatus) &&          $WinterStatus=="open") echo "checked";?>  value="Open">Open
   <input type="radio" name="WinterStatus" <?php if (isset($WinterStatus) && $WinterStatus=="closed") echo "checked";?>  value="Closed">Closed
   <span class="error"><font color="red"><b>* <?php echo $WinterStatusErr;?></b></font></span>
   <br><br>
   <input type="submit" name="submit" value="Submit" <?php
if ($Winterstatus == "Open")
    { copy ("images/Open.jpg", "images/status/Status.jpg"); }
   elseif ($Winterstatus == "Closed")
    { copy ("images/Closed.jpg", "images/status/Status.jpg");}
?>> 
</form>

<?php echo $WinterStatus ?>
</body>

首先从您的提交输入中删除if语句,然后将if语句中的$ Winterstatus更改为$ WinterStatus。

in php you can use exec() to execute a command. on linux the "cp" command should do what you are asking. Permission/security issues will be a problem though, so i would recommend just changing a variable to the new image rather then actually moving it...a lot simpler and safer -- if I knew where you reference the image I could help you figure that out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM