skip to Main Content

In my website there is, 6 digit random numbers known as refno Example 20221234and 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 refnoin 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


  1. <?php
    if(isset($_POST['saveimage']))
    {
        $directory="images/";
        $filename =$_FILES['file']['name'];
        $filename_temp =$_FILES['file']['tmp_name'];
        $new_filename = $directory.date("YmdHis")."_".$filename;
        
        if(move_uploaded_file($filename_temp,$new_filename))
            {
                echo "File Uploaded ";
            }
        else
            {
                echo "File Upload Error.";
            }
    }
    echo "<hr/>";
    ?>
     <form method="post" action="<?php $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
     <input type="file" name="file" />
     <input type="submit" name="saveimage" value="save" />
     </form>
    
    Login or Signup to reply.
  2. <?php    
        if(isset($_POST['submit']))
        {
        
            $refno = isset ($_GET['refno'])? $_GET['refno']:'';
            $file_tmp = 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"] : "";
            
            //Images Directory
            $dataDir = "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/";
            
            //New Filename
            $new_filename = $dataDir.$refno.$file_name;
            
            //File formats
            $file_formats=array("jpeg","jpg","png");
            
            //echo $file_size;
            if($file_size>0){
                //echo "Good";
                $file_info = pathinfo($file_name);
                $file_ext= $file_info['extension'];
                
                //check the extension
                if(in_array($file_ext,$file_formats))
                    {
                        //Database Connection 
                        //$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd, $database) or die ("Unableeeee to connect!");
                        if(move_uploaded_file($file_tmp, $new_filename))
                        {
                            $message="alert('Visual Defect Report and pictures are successfully submitted!')";
                        }
                        else
                        {
                            $message= "  alert('Error Uploading File!')";
                        }                   
                        
                    }
                else
                    {
                        $message= "  alert('Invalid Picture Format!')";
                    }
            }
            else
            {
                $message= "  alert('No picture attached!')";
            }
                     
            echo "<script language="javascript" type="text/javascript">";
            echo $message;
            //echo $refno;
            echo "</script>";
        }
    
    ?> 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search