PHP Upload IF ISSET always says it is?

孤街醉人 提交于 2019-11-29 11:23:13

To check if the file has been uploaded, how about:

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
{
    echo 'No upload';
}
else
{
    // Your file has been uploaded
}

is_uploaded_file() is the choice here, check the docs out for it.

$_FILES is an array of files, and each file in the first dimension of the array is automatically given a ['tmp_name'] key in the second dimension, with the value being it's temporary location.

Obviously you then need to use move_uploaded_file() on the temporary file.

You are checking only that $_FILES['image'] passed from your form.
But you are not checking that posted $_FILES['image'] is empty or its contains a valid file data
Read more here http://www.php.net/manual/en/features.file-upload.php

<?php
  if ( isset( $_FILES["image"] ) && !empty( $_FILES["image"]["name"] ) ) {
    if ( is_uploaded_file( $_FILES["image"]["tmp_name"] ) && $_FILES["image"]["error"] === 0 ) {
      // everything okay, do process
      echo 'yes';
      exit();
    }
  }
  echo 'no';
?>

The reason "isset" doesn't work is most likely because $_FILES is considered set even when it is set to "null". I found the empty() suggestion to be the cleanest. Although, unless you're using a large form with multiple files, I would remove the brackets entirely.

if (empty ($_FILES)){
 echo 'No file';
 } else {    
 echo 'File found';
 }

Use empty() instead.

if (!empty ($_FILES['image'])){
    echo 'yes';


} else {    
    echo 'no';      
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!