php上传状态显示附件

php上传状态显示附件,php,Php,我已经用php创建了一个可以正常工作的上传脚本,它工作得很好。但我有一个想法,将上传状态附加到php脚本。上传状态也是php的,并且有一个任何上传图像都应该显示的演示值。 这是上传状态的代码 //create the upload $total = rand(1, 5000);// 5000 is the proposed image size in kb $current = rand(1, $total); $percent = round(($current/$total) *

我已经用php创建了一个可以正常工作的上传脚本,它工作得很好。但我有一个想法,将上传状态附加到php脚本。上传状态也是php的,并且有一个任何上传图像都应该显示的演示值。 这是上传状态的代码

    //create the upload 
$total = rand(1, 5000);// 5000 is the proposed image size in kb
$current = rand(1, $total);
$percent = round(($current/$total) * 100, 1);


   echo  "$current is $percent% of $total.<p/>";
//创建上传
$total=兰特(15000);//5000是建议的图像大小(kb)
$current=兰特(1$total);
$percent=四舍五入(($current/$total)*100,1);
echo“$current是$total的$percent.

”;

这是上传状态的css

<style type="text/css">

  .outter{
     height: 25px;
     width: 500px;
     border: solid 1px #000;

  }

  .inner{
    height:25px;
    width:<?php echo $percent ?>%;
    border-right: solid 1px #000;
    background: #f8ffe8; /* Old browsers */
    background: -moz-linear-gradient(top, #f8ffe8 0%, #e3f5ab 33%, #b7df2d 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8ffe8), color-stop(33%,#e3f5ab), color-stop(100%,#b7df2d)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%); /* IE10+ */
    background: linear-gradient(to bottom, #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f8ffe8', endColorstr='#b7df2d',GradientType=0 ); /* IE6-9 */

  }

  </style>

奥特先生{
高度:25px;
宽度:500px;
边框:实心1px#000;
}
.内部{
高度:25px;
宽度:%;
右边框:实心1px#000;
背景:#f8ffe8;/*旧浏览器*/
背景:-莫兹线性梯度(顶部,#f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);/*FF3.6+*/
背景:-webkit渐变(线性、左上、左下、颜色停止(0%、#f8ffe8)、颜色停止(33%、#e3f5ab)、颜色停止(100%、#b7df2d));/*铬、Safari4+*/
背景:-webkit线性梯度(顶部,#f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);/*铬10+,Safari5.1+*/
背景:-o-线性梯度(顶部,#f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);/*Opera 11.10+*/
背景:-ms线性梯度(顶部,#f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%;/*IE10+*/
背景:线性梯度(至底部,#f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%;/*W3C*/
过滤器:progid:DXImageTransform.Microsoft.gradient(startColorstr='#f8ffe8',endColorstr='#b7df2d',GradientType=0);/*IE6-9*/
}
上载状态的html:

<div class="outter">
    <div class="inner"><?php echo $percent ?>%</div>

  </div>

%
我有一个工作上传php脚本,它上传图像到指定的文件夹。代码如下

html

<!DOCTYPE html>
<html>
<body>

<form action="" method="post"  enctype="multipart/form-data">


  <p>
    <label for="file">Filename:</label><br>
    <input type="file" name="fileToUpload" id="fileToUpload">
    </p>

    <p>
    <input type="submit" value="Upload Image" name="upload">
    </p>


</form>


文件名:

并且php脚本是

  $target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));





// Check if image file is a actual image or fake image

if(isset($_POST["upload"])) {

  if($_FILES['fileToUpload']['size'] == 0  ){ // validate file input 

     echo '<p style="color: red;">file empty</p>';
  }else{



   $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);



    if($check !== false) {
        echo "<p style='color: red;' >File is an image - " . $check["mime"] . ".</p>";
        $uploadOk = 1;
    } else {
        echo "<p style='color: red;'>File is not an image.</p>";
        $uploadOk = 0;
    }

// Check if file already exists
if (file_exists($target_file)) {
    echo "<p style='color: red;'>Sorry, file already exists.</p>";
    $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "<p style='color: red;'>Sorry, your file is too large.</p>";
    $uploadOk = 0;
}


// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "<p style='color: red;'>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
    $uploadOk = 0;
}


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<p style='color: red;'>Sorry, your file was not uploaded.</p>";


// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "<p style='color: green;' >The file <font>". basename( $_FILES["fileToUpload"]["name"]). "</font> has been uploaded.".basename( $_FILES["fileToUpload"]["name"])."</p>";



     } else {
        echo "<p style='color: red;'>Sorry, there was an error uploading your file.</p>";
      }

   }



   }





  } 
$target_dir=“images/”;
$target\u file=$target\u dir。basename($_文件[“fileToUpload”][“name”]);
$uploadOk=1;
$imageFileType=strtolower(路径信息($target_文件,路径信息_扩展名));
//检查图像文件是真实图像还是假图像
如果(isset($_POST[“upload”])){
如果($_FILES['fileToUpload']['size']==0){//验证文件输入
echo'

文件为空; }否则{ $check=getimagesize($_文件[“fileToUpload”][“tmp_名称]); 如果($check!==false){ echo“

文件不是图像。

”; $uploadOk=0; } //检查文件是否已经存在 如果(文件_存在($target_文件)){ echo“

对不起,您的文件太大。

”; $uploadOk=0; } //允许某些文件格式 如果($imageFileType!=“jpg”&&&$imageFileType!=“png”&&&$imageFileType!=“jpeg” &&$imageFileType!=“gif”){ echo“

抱歉,您的文件未上载。

”; //如果一切正常,请尝试上载文件 }否则{ 如果(移动上传的文件($文件[“fileToUpload”][“tmp文件名”],$target文件)){ echo“

很抱歉,上载文件时出错。

”; } } } }
所以现在我没能做到的是 1。获取上传图像的实际存储。 2.将实际图像存储附加到
$total
中,并将上载序列附加到实际上载工作上载脚本中


我需要整个脚本作为一个整体工作

如果我理解正确,您希望显示文件的上载进度

据我所知,在PHP中无法做到这一点,因为PHP脚本在文件上传到服务器(通常在临时文件夹中)后获得控制权

但是,可以通过XHRAPI获得百分比(要求客户机启用javascript):


任何主体都可以使用这个示例并获得java脚本或jquery上传状态吗?am中继已超出该javascript范围。
let xhr = new XMLHttpRequest()

// ...

xhr.upload.addEventListener('progress', (evt) => {
    if (evt.lengthComputable) {
        const percentage = (evt.loaded / evt.total) * 100
    }
}, false)