简体   繁体   English

如果代码中检查了上传文件,则上传文件不起作用

[英]Upload file not working if checking for uploaded files is on the code

The images are uploading normally, but I want an "else if" to check if there is any file selected. 图像正常上载,但是我想用“ else if”检查是否选择了任何文件。 This is working: 这正在工作:

    <?php
session_start();
    include('includes/conexao.php');
    $fileinfo=PATHINFO($_FILES["image"]["name"]);
    $newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
    move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
    $location="images/perfis/" . $newFilename;

$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");

if( mysqli_num_rows($todas_fotos) > 0) {
        //$path=$location;
        //if(unlink($path)) echo "Deleted file ";
        mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
}
else if( mysqli_num_rows($todas_fotos) == 0)
{
        mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
}
else {

};
    header('location:perfil.php');
?>

It inserts if there isn't an image, but if there is, it updates. 如果没有图像,它将插入,但是如果存在,它将更新。 But when I add: 但是当我添加:

else if (empty($_FILES['image']['name']))
{
header('location:perfil.php');
}

It returns me undefined index: extension on line 5. How to go? 它返回了我未定义的索引:第5行的扩展。怎么走?

Rewrite the code as follows. 如下重写代码。 Here we are checking whether the file is not available at the beginning of the code and redirect if no file is found. 在这里,我们在代码开头检查文件是否不可用,如果找不到文件,则重定向。

<?php
session_start();
if (empty($_FILES['image']['name']))
{
  header('location:perfil.php?error=1');
  return;
}
include('includes/conexao.php');
$fileinfo=PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
$location="images/perfis/" . $newFilename;

$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE 
img_usu_codigo = '" . $_SESSION['codigo'] . "'");

if( mysqli_num_rows($todas_fotos) > 0) {
    //$path=$location;
    //if(unlink($path)) echo "Deleted file ";
    mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
 }else {
    mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
 }

and in the perfil.php you should put 并且在perfil.php中,您应该放

window.onload = function(){ var url = new URL(window.location.href); window.onload = function(){var url = new URL(window.location.href); var error = url.searchParams.get("error"); var错误= url.searchParams.get(“错误”); if(error==1) alert("No file uploaded"); if(error == 1)alert(“未上传文件”);

} }

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

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