Check if php file is uploaded

Asked 2 years ago, Updated 2 years ago, 40 views

I'm making a bulletin board, but I want to check if the file is attached in the file upload, but it's not working well.ㅠ<

write.html

<div class="banner">
                <form enctype="multipart/form-data" action="php/Write_Update.php?location=<?php echo $location ?>" method="POST">
                    <div class="post">
                        <div class="carteory">
                            <select name="Carteory">
                                <option value="Overall">Overall</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                        <div class="post-title">
                            <h3>Title</h3>
                            <input type="text" name="title" placeholder="Please enter a title.">
                        </div>
                        <div class="post-content">
                            <h3>Content</h3>
                            <textarea name="content" placeholder="Please enter content."></textarea>
                        </div>
                        <div class="post-tag">
                            <h3>Tags</h3>
                            <input type="text" name="tag" placeholder="#">
                        </div>
                        <div class="FileupBtn">
                            <input type="file" name="profile">
                        </div>
                        <div class="Btn-up">
                            <Button class="uploadBton">Create</Button>
                        </div>
                    </div>
                </form>
            </div>

write_update.php

if($member['User_Log']==1) {
      // // print "if";
      $Carteory = $_POST['Carteory'];
      $Title = $_POST['title'];
      $Content = $_POST['content'];
      $date = date('Y-m-d H:i:s');
      $Tag = $_POST['tag'];

      if(isset($_FILES['profile'])) {
         print $_FILES['profile'];
         // // $imagecheck = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
         $imgpath = pathinfo($_FILES['profile']['name']);

         $file_name = $_FILES['profile']['name']; // the filename you uploaded
         $file_tmp_name = $_FILES['profile']['tmp_name']; // Filename stored in temporary directory
         $file_size = $_FILES['profile']['size']; // Size of uploaded file
         $mimeType = $_FILES['profile']['type']; // MIME Type of uploaded file

         // Specify the server directory where attachments will be stored
         $save_dir = '../uploadFile/';

         // Scan for upload file extensions
         // // if(in_array($mimeType, $imagecheck)) {
         //       echo("<script> alert('Unable to upload file format.'); </script>");  
         //       //       echo("<script>location.href='../Write.html';</script>");
           //       //       exit;
         // } 

         // Change file name
       $real_name = $file_name; // Original filename (actual filename before uploading) 
          $arr = expand(".", $real_name); // Get the extension of the original file and apply it as it is $file_exe    
       $file_exe = $mimeType;
         $arr1 = $arr[0];   
         $arr2 = $arr[1];   
         $arr3 = $arr[2];
         $arr4 = $arr[3];   
         if($arr4) {
            $file_exe = $arr4;
         } } else if($arr3 && !$arr4) { 
            $file_exe = $arr3;                  
         } } else if($arr2 && !$arr3) {
            $file_exe = $arr2;                  
         }

       $file_time = time(); 
        $file_Name = "file_".$file_time.".".$file_exe; // Create a file name to be actually uploaded
        $change_file_name = $file_Name; // Specify the changed filename in the variable 
        $real_name = addslashes($real_name); // Original filename to be uploaded (real filename before uploading) 
        $real_size = $file_size; // the size of the file being uploaded

         //The full path to the directory and filename where you want to store the file
         $dest_url = $save_dir . $change_file_name;

         //Upload file to specified directory
         if(!move_uploaded_file($file_tmp_name, $dest_url))  {
            die ("Failed to upload file to specified directory")
         }
      }
      // // else {
      //    //    die("else");
      // }

      $sql2 = "insert into " . $db_location . "(id, carteory, title, content, date, tag, change_file_name, real_name, real_size) values 
      ('$id', '$Carteory', '$Title', '$Content', '$date', '$Tag', '$change_file_name', '$real_name', '$real_size')"; 

      $result = mysqli_query($conn, $sql2);

      If($result) { //query is successful,
         echo("<script> alert('The writing was successfully registered.'); location.href='../$web_location';</script>");
      }
      else {
         echo("<script> alert(') failed to register.'); location.href='../Write.html?location=$db_location';</script>");
      }
   }

Part of the source code.

in write_update.php If(isset($_FILES['profile']))

I want to check whether the file is attached or not, but it comes out as true no matter what.

What should I do?

html php mysql

2022-09-21 11:31

1 Answers

index.php

<form enctype="multipart/form-data" action="Write_Update.php" method="POST">
<input type="file" name="profile">
<Button class="uploadBton">Create</Button>
</form>

Write_update.php

        if(isset($_FILES['profile']))
        {
              echo "ISSET  ";
        }

        if(isset($_FILES['profile']) && $_FILES['profile']['name'] != "")
        {
              echo $_FILES['profile']['name'];
              echo "upload";
        }
        else
        {
              echo "cannot upload";
        }

When transferring files without uploading them: ISSET cannot upload

When uploading and transferring a file: ISSET [file name] upload


2022-09-21 11:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.