In my website there is, 6 digit random numbers known as refno
Example 20221234
and there is fileupload. Right now, it saves the photos with its own name on to the another filepath examplecat.png
. Currently, I tried to add that refno
in front of the picture while saving like20221234cat.png
. Is that possible to do?
<?php
$refno = isset ($_GET['refno'])? $_GET['refno']:'';
$file = isset($_FILES["file"]["tmp_name"])? $_FILES["file"]["tmp_name"] : "";
$file_size = isset($_FILES["file"]["size"])? $_FILES["file"]["size"] : "";
$file_name = isset($_FILES["file"]["name"])? $_FILES["file"]["name"] : "";
if(isset($_POST['submit']))
{
$dataDir = "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/";
if ($file_size <= 0)
{
echo "<script language="javascript" type="text/javascript">";
echo " alert('No picture attached!')";
//echo $refno;
echo "</script>";
}
else
{
if(stristr($file_name, ".png")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language="javascript" type="text/javascript">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
else if(stristr($file_name, ".jpg")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language="javascript" type="text/javascript">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
else if(stristr($file_name, ".jpeg")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language="javascript" type="text/javascript">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
}
}
$file_name= "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/" . $refno.$file_name;
flush();
mysqli_close($conn);
?>
Below is how I get the refno
.
<script type="text/javascript">
const now = new Date();
let randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-2);
window.onload = function () {
document.getElementById("refno").value = `${new Date().getFullYear()}${randomNum}`;
}
</script>
<label class="control-label col-sm-4" for="refno">REF nos :</label>
<div class="col-sm-4">
<p class="form-control-static" style="margin-top: -6px;">
<input type="text" class="form-control" id="refno" name="refno" value="<?php echo $refno;?>" disabled>
</p>
</div>
2
Answers