简体   繁体   English

不能跑<Script> after redirection from php

[英]Can't run <Script> after redirection from php

I have two files named index.php and uploadcover.inc.php. 我有两个名为index.php和uploadcover.inc.php的文件。 Everything is working fine, except the script tags being executed if the upload fails for any of the if-else conditions. 一切工作正常,但如果在任何if-else条件下上传均失败,则执行脚本标签除外。 Here is the code: 这是代码:

index.php => index.php =>

      <form action="include/uploadcover.inc.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="cover-upload" style="display:none" onchange="this.form.submit();">
        <label for="cover-upload" class="fa fa-camera fa-2x" aria-hidden="true"></label>
      </form>

uploadcover.inc.php => uploadcover.inc.php =>

<?php
session_start();
include_once 'dbh.inc.php';
$sessionid = $_SESSION['u_id'];

    $filename = "../profile/cover".$sessionid.".*";
    $fileinfo = glob($filename);
    $fileExt= explode('.',$fileinfo[0]);
    $fileActualExt= $fileExt[3];
    $file = "../profile/cover".$sessionid.".".$fileActualExt;
    if(!unlink($file)){
      echo "File not deleted";
    } else {
      "File deleted";
    }

    $sql = "UPDATE coverimg SET status=1 WHERE user_id='$sessionid';";
    mysqli_query($conn,$sql);

  $file= $_FILES['file'];
  $fileName= $file['name'];
  $fileTmpName= $file['tmp_name'];
  $fileSize= $file['size'];
  $fileError= $file['error'];
  $fileType= $file['type'];

  $fileExt = explode('.',$fileName);
  $fileActualExt = strtolower(end($fileExt));

  $allowed = array('jpg','jpeg','png','gif');

  if(in_array($fileActualExt,$allowed)){
    if($fileError=== 0){
      if($fileSize<3145728){
        $fileNameNew = "cover".$sessionid.".".$fileActualExt;
        $fileDestination = '../profile/'.$fileNameNew;
        move_uploaded_file($fileTmpName,$fileDestination);
        $sql = "UPDATE coverimg SET status=0 WHERE user_id='$sessionid'";
        mysqli_query($conn,$sql);
        header("Location: ../index.php?upload=success");
      } else {
        header("Location: ../index.php?upload=size_exceeded_3MB");
        exit();
        echo "<script>alert('File should be less than 3MB!')</script>";
      }
    } else {
      header("Location: ../index.php?upload=error");
      exit();
      echo "<script>alert('Error uploading the file!')</script>";
    }
  } else{
    header("Location: ../index.php?upload=typeerror");
    exit();
    echo "<script>alert('Filetype not supported!')</sc
    ript>";
  }

Note: I tried die(), exit() and also removing them in case it executes the script after being redirected to index.php but it doesn't work. 注意:我尝试过die(),exit()并删除它们,以防重定向到index.php后执行脚本,但是它不起作用。

A redirect tells the client that why they are looking for can be found at a different URL. 重定向告诉客户端可以在其他URL上找到他们要查找的原因。 The client then requests that other URL and displays that document instead. 然后,客户端请求该其他URL并显示该文档。

You can't simultaneously say "Here is the document you should show" and "The document you should show can be found over here". 您不能同时说出“此处应显示的文档”和“此处应显示的文档”。 It is one or the other. 是一个或另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM