无法运行< Script>从PHP重定向后 [英] Can't run <Script> after redirection from php

查看:90
本文介绍了无法运行< Script>从PHP重定向后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为index.php和uploadcover.inc.php的文件。一切工作正常,除非脚本标记正在执行,如果任何if-else条件上传失败。这里是代码:

index.php =>

 < ; form action =include / uploadcover.inc.phpmethod =postenctype =multipart / form-data> 
< input type =filename =fileid =cover-uploadstyle =display:noneonchange =this.form.submit();>
< label for =cover-uploadclass =fa fa-camera fa-2xaria-hidden =true>< / label>
< / form>

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)){
echoFile not deleted;
} else {
文件已删除;
}

$ 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');
$ b $如果(in_array($ fileActualExt,$允许)){
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>;

$ / code>

注意:我试过die(),exit()如果它在重定向到index.php后执行脚本,但它不起作用。 重定向告诉客户端: 为什么他们正在寻找可以在不同的网址找到。客户然后请求其他URL并显示该文档。



您不能同时说这是您应该显示的文档和您应该显示的文档可以在这里找到。它是一个或另一个。


I have two files named index.php and uploadcover.inc.php. Everything is working fine, except the script tags being executed if the upload fails for any of the if-else conditions. Here is the code:

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 =>

<?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.

解决方案

A redirect tells the client that why they are looking for can be found at a different URL. The client then requests that other URL and displays that document instead.

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.

这篇关于无法运行&lt; Script&gt;从PHP重定向后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆