php - 文件上传按钮在翻转卡上不起作用

我已经完成了 html 和 PHP 代码的文件 uploader 按钮,但是当我尝试测试它以查看它是否可以在浏览器中工作时,我从文件夹中选择了一个图像,然后我选择了我选择的任何图像“提交”翻页卡上什么都没有出现,我不确定为什么什么都没有出现

<!--flip card code.-->
      <html>
                
               <head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
 body {
  font-family: Arial, Helvetica, sans-serif;
}

.flip-card {
  background-color: transparent;
  width: 300px;
  height: 300px;
  perspective: 1000px;
}

.flip-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.6s;
  transform-style: preserve-3d;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
}

.flip-card:hover .flip-card-inner {
  transform: rotateY(180deg);
}

.flip-card-front, .flip-card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.flip-card-front {
  background-color: #bbb;
  color: black;
}

.flip-card-back {
  background-color: #2980b9;
  color: white;
  transform: rotateY(180deg);
}
</style>
</head>
<body>
<h1>Card Flip with Text</h1>
<h3>Hover over the image below:</h3>
<div class="flip-card">
  <div class="flip-card-inner">
     <div class="flip-card-front">
      </div>
      <div class="flip-card-back">                                 
      </div>
</div>
</div>
</body>
</html>
                <!--Flip card code-->
                                

                                <!-- File uploader button code-->
<html>
<body>

<form action="action_page.php" method="post" enctype="multipart/form-data">
  Select image to upload:
    <input type="file" name="file"> 
    <button type="submit" name="submit">UPLOAD</button>
</form>

</body>
</html>
<!--action_page.php code-->

<?php
if(isset($_POST["submit"])) {
    $file = $_FILES['file'];
    
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
    
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');
    
    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0){
            if($fileSize < 100000){
                $fileNameNew = uniqid('', true).".".$fileActualExt;
                fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file(fileTmpName, $fileDestination);
                header("Location: index.php?uploadsuccess");
                
            } else {
                echo "Your file is too big!";
            }
            
        } else {
            echo "There was an error uploading your file";   
        }
        
    } else { 
        echo "You cannot upload files of this type!";
    }
}
?>
<!-- The core Firebase JS SDK is always required and must be listed first -->
                <script src="/__/firebase/7.14.2/firebase-app.js"></script> <!--specifies the URL of an external script file-->
                
                <!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
                
                <script src="/__/firebase/7.14.2/firebase-analytics.js"></script> <!--specifies the URL of an external script file-->
                
                <!-- Initialize Firebase -->
                <script src="/__/firebase/init.js"></script> <!--specifies the URL of an external script file-->
                <script type="text/javascript" src="cordova.js"></script> <!--specifies the media type of the script-->
                <script type="text/javascript" src="js/index.js"></script> <!--specifies the media type of the script--></body>
        </html>

最佳答案

除了“fileActualExt”之外一切都很好!查看您忘记连接实际扩展名的 $fileNameNew。相反,您编写的 fileActualExt 就像一个常量。

正确的代码是$fileNameNew = uniqid('', true).".".$fileActualExt;

不要忘记在 action_page.php 文件所在的目录中创建 uploads 文件夹。

<?php
if(isset($_POST["submit"])) {
    $file = $_FILES['file'];
    
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
    
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');
    
    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0){
            if($fileSize < 100000){
                $fileNameNew = uniqid('', true).".".$fileActualExt; // concatination of uniqueid and fileActualExt
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header("Location: index.php?uploadsuccess");
                
            } else {
                echo "Your file is too big!";
            }
            
        } else {
            echo "There was an error uploading your file";   
        }
        
    } else { 
        echo "You cannot upload files of this type!";
    }
}
?>

https://stackoverflow.com/questions/63269712/

相关文章:

postgresql - 使用 Aurora postgres serverless 在 AWS R

reactjs - 我需要将浏览器路由器一分为二

docker - SMTP 未配置,请检查您的 grafana.ini 配置文件的 [smtp] 部

text - 关注 VStack 中的下一个 TextField

css - Angular ngx-bootstrap - 如何制作一个又大又宽的模态

python-3.x - XGboost 目标 : Is there a way to do Mul

postgresql - Postgres JSONB 检索非常慢

python - Pandas 在 Linux 中失败,在 Windows 中没有发生 - 缺少数据

perl - 为什么 local 在 STDERR 和 STDOUT 上不起作用?

c# - 解析值行 1,位置 1 时遇到意外字符