如何使用php上传表单时附加进度条

时间:2016-08-11 08:18:57

标签: javascript php html5 progress-bar image-uploading

我有一个表单,用于将带有标题的图像上传到服务器,成功上传后,它会将您重定向到另一个页面。上传图片时,我想在那里显示进度条,并在成功上传后重定向到另一个页面 我知道PHP,也谷歌搜索进度条。所有链接仅与javascript相关,我是javascript的新手。有人可以帮我这个。 这是upload.php包含表单

<form action="newupload.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <div id="image-cropper">
            <div class="cropit-preview"></div>
            <input type="file" name="user_image" class="cropit-image-input" style="font-weight: bold;" />
        </div>
    </div>
    <div class="form-group">
        <input type="text" name="title" class="input-round big-input" id="title-modal" placeholder="Enter your Image Title" required/>
        <input type="hidden" name="category" value="<?php echo $category; ?>" />
        <input type="hidden" name="hashtag" value="<?php echo $hashtag; ?>" />
    </div>
    <div class="form-group">
        <!-- required  -->
        <span class="required margin-three">*Please complete all fields correctly</span>
        <!-- end required  -->
        <p class="text-center">
            <!-- button  -->
            <button class="btn btn-black no-margin-bottom btn-medium btn-round no-margin-top" type="submit">Submit</button>
            <!-- end button  -->
        </p>
    </div>
</form>

这是用于移动和更新数据库的newupload.php文件

<?php
define("MAX_SIZE", "400");
include ('mysqli_connect.php');

session_start();
$email = $_SESSION['user_email'];
$q = "select * from user where u_email='$email'";
$qres = mysqli_query($dbc, $q);
$qrow = mysqli_fetch_array($qres, MYSQLI_ASSOC);
$u_id = $qrow['u_id'];

function getExtension($str)
  {
  $i = strrpos($str, ".");
  if (!$i)
    {
    return "";
    }

  $l = strlen($str) - $i;
  $ext = substr($str, $i + 1, $l);
  return $ext;
  }

if ($_SERVER["REQUEST_METHOD"] == "POST")
  {
  $image = $_FILES['user_image']['name'];
  $uploadedfile = $_FILES['user_image']['tmp_name'];
  $image_title = $_POST['title'];
  $category = $_POST['category'];
  $hashtag = $_POST['hashtag'];
  if ($image)
    {
    $filename = stripslashes($_FILES['user_image']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
      {
      echo ' Unknown Image extension ';
      $errors = 1;
      }
      else
      {
      $size = filesize($_FILES['user_image']['tmp_name']);
      if ($extension == "jpg" || $extension == "jpeg")
        {
        $uploadedfile = $_FILES['user_image']['tmp_name'];
        $src = imagecreatefromjpeg($uploadedfile);
        }
        else
      if ($extension == "png")
        {
        $uploadedfile = $_FILES['user_image']['tmp_name'];
        $src = imagecreatefrompng($uploadedfile);
        }
        else
        {
        $src = imagecreatefromgif($uploadedfile);
        }

      $maxwidth = 800;
      list($width, $height) = getimagesize($uploadedfile);
      if ($width >= $maxwidth)
        {
        $newwidth = 800;
        $newheight = ($maxwidth / $width) * $height;
        }
        else
        {
        $newwidth = $width;
        $newheight = $height;
        }

      $tmp = imagecreatetruecolor($newwidth, $newheight);
      imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      $t = uniqid();
      $filenamee = 'user-' . $t;
      $path = 'uploaded/';
      $filename = $path . $filenamee . ".jpg";
      move_uploaded_file($_FILES['user_image']['tmp_name'], $filename);
      (u_id, s_category, s_title, s_upload, s_maxupload) values('$u_id', '$category', '$image_title', '$filename', '$mfilename') ";
 $query="insertintoselfiepost(u_id, s_category, s_title, s_upload, s_hashtag, upload_time) values('$u_id', '$category', '$image_title', '$filename', '$hashtag', now()) ";
    $res=mysqli_query($dbc,$query);
    if($res){

        header('Location:home.php?insert=1');
   } else{
            header('Location:home.php?insert=0');
        }
    }}
    }
    ?>

1 个答案:

答案 0 :(得分:0)

当你从计算机上传内容到服务器(或称为下载)时,不可能在PHP中上传进度条,因为它是服务器端。

有效的方法是在javascript和PHP中完成。

也许你可以看看这里:https://www.sitepoint.com/tracking-upload-progress-with-php-and-javascript/