[英]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. 我刚开始使用PHP,并且正在尝试为客户提供一种方法,使其能够使用单选按钮和表单提交来更改图像。 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.
我的意图是将2个图像文件(Open / Closed.jpg)中的一个复制并重命名到另一个位置(images / status / Status.jpg),以便不需要进行编码即可进行更改。
The "new" image will then be used elsewhere. 然后,“新”图像将在其他地方使用。 I have tried various copy() and rename() calls, but with no luck.
我尝试了各种copy()和named()调用,但是没有运气。 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. 在php中,您可以使用exec()执行命令。 on linux the "cp" command should do what you are asking.
在Linux上,“ cp”命令应该执行您所要求的操作。 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.
权限/安全性问题将是一个问题,因此我建议仅将变量更改为新图像,而不是实际将其移动...更简单,更安全-如果我知道您引用图像的位置,那么我可以帮助您确定那出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.