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

时间:2018-09-19 15:09:36

标签: php file-upload

图像正常上传,但是我希望“否则”检查是否选择了任何文件。工作正常:

Promise.all([p1,p2])

如果没有图像,它将插入,但是如果有图像,它将更新。但是当我添加时:

let drinkObservable = this.drinkService.getAllDrinks()
let paramsObervable = this.route.params

let sub = combineLatest(drinkObservable, paramsObervable)
  .pipe(switchMap(([drinks, params]) => {
    this.allDrinks = drinks
    let filteredDrinks = this.filterService.filter(drinks, params['filter'], params['name']);
    return this.sorter.initialize(filteredDrinks)
  }))
  .pipe(switchMap(sortedDrinks => {
    return this.pagerService.initializePaginatedItems(sortedDrinks, 10, 5)
  }))
  .subscribe(pager => {
    this.pager = pager;
    this.paginatedDrinks = pager.paginatedItems;
  })

它返回我未定义的索引:第5行的扩展名。怎么走?

1 个答案:

答案 0 :(得分:1)

重写代码,如下所示。在这里,我们在代码开头检查文件是否不可用,如果找不到文件,则会重定向。

<?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'] . "')");
 }

,并且应该在perfil.php中放置

     window.onload = function(){      var url =新的URL(window.location.href);      var错误= url.searchParams.get(“错误”);      如果(错误== 1)         alert(“未上传文件”);

}