2017-09-02 65 views
0

这是我upload.php的文件 这里是代码注意:未定义指数:文件C: XAMPP htdocs中项目上传 upload.php的第6行

<?php 


include_once 'dbh.php'; 

$target_dir = "project/"; 
$target_file = $target_dir . basename($_FILES["file"]["name"]); 

$uploadOk = 1; 

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

// Check if image file is a actual image or fake image 

if(isset($_POST["submit"])) { 


    $check = getimagesize($_FILES["file"]["tmp_name"]); 

    if($check !== false) { 

     echo "File is an image - " . $check["mime"] . "."; 

     $uploadOk = 1; 

    } else { 
     echo "File is not an image."; 

     $uploadOk = 0; 

    } 
} 
?> 

这是我的HTML代码

<form action="upload.php" method="post" enctype="multipart/form-data"> 

    <label class="custom-file-input" for="file" > 
    </label> 

    <input id="file" type="file" multiple="multiple" name="file" accept="image/*" style="visibility: hidden"> 
    </form> 

我不知道为什么它显示我此错误

公告:未定义的索引:文件C:\ XAMPP \ htdocs中\项目\ upload.p惠普在第6行任何帮助将提前感激!

+0

HTML代码<形式行动= “upload.php的” 方法= “POST” ENCTYPE = “多部分/格式数据”> <标签类=“定做文件输入“for =”文件“> Hafsa

+0

由于您在'$ _FILES'中可用之前访问'file' – JYoThI

+0

** 1st:**因为在它可用之前访问'file'在'$ _FILES'中。 **第二:**将您的代码移入'isset()'中。 **第三:**您需要添加提交输入。 '' – JYoThI

回答

0

@Darren H:$ _FILES是超全局的,这是正确的,但是如果没有发送表单,那么HTML表单中的文件输入没有设置。在这种情况下,$ _FILES是空的,$ _FILES ['file']是未定义的索引。

0

使用以下代码:

<?php 


include_once 'dbh.php'; 



// Check if image file is a actual image or fake image 

if(isset($_POST["submit"])) { 
$target_dir = "project/"; 
$target_file = $target_dir . basename($_FILES["file"]["name"]); 

$uploadOk = 1; 

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 


    $check = getimagesize($_FILES["file"]["tmp_name"]); 

    if($check !== false) { 

     echo "File is an image - " . $check["mime"] . "."; 

     $uploadOk = 1; 

    } else { 
     echo "File is not an image."; 

     $uploadOk = 0; 

    } 
} 
?> 
相关问题