簡體   English   中英

如何使用PHP,提交和單選按鈕復制/重命名/覆蓋圖像?

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

我剛開始使用PHP,並且正在嘗試為客戶提供一種方法,使其能夠使用單選按鈕和表單提交來更改圖像。 我的意圖是將2個圖像文件(Open / Closed.jpg)中的一個復制並重命名到另一個位置(images / status / Status.jpg),以便不需要進行編碼即可進行更改。

然后,“新”圖像將在其他地方使用。 我嘗試了各種copy()和named()調用,但是沒有運氣。 以下是我正在使用的代碼。 我覺得我的問題出在提交調用上,但是我嘗試過的所有方法都沒有用。 因此,我回到原始代碼(如下),在經驗豐富的用戶/程序員的幫助下嘗試重新開始。

<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。

在php中,您可以使用exec()執行命令。 在Linux上,“ cp”命令應該執行您所要求的操作。 權限/安全性問題將是一個問題,因此我建議僅將變量更改為新圖像,而不是實際將其移動...更簡單,更安全-如果我知道您引用圖像的位置,那么我可以幫助您確定那出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM