0

收到此错误我该如何解决?我只需要指出正确的方向

注意:未定义索引:第 36 行 C:\Users\chisenga\Documents\project\htdocs\project\upload.php 中的 file_upload

这是代码

 form method="post" enctype="multipart/form-data" action="upload.php">
<label for="">File: <input type="file" name="file_upload"/></label>
<input type="submit" value="Upload"/>
</form>


<?php

$file = $_FILES['file_upload'];
$name = $file['name'];
$type = $file['type'];
$tmp_location = $file['tmp_name'];
$upload = 'uploads';
$final_destination = $upload.'/'.$name;
$error = $file['error'];
$max_upload_size = 2097152;
$size = $file['size'];


$allowedImageTypes = array( 'image/png', 'image/jpeg', 'image/gif' );

function imageTypeAllowed($imageType){
global $allowedImageTypes;
if(in_array($imageType, $allowedImageTypes)){
    return true;
}
else{
    return false;
}
}

 //Check for errors
 if($error > 0 || is_array($error)){
 die("Sorry an error occured");
}

//Check if file is image
//Only required if image is only whjat we need
//if(!getimagesize($tmp_location)){
//die("Sorry, you can only upload image types");
//}

 if(!imageTypeAllowed($type)){
  die("Sorry, file type is not allowed");
 }

 if(file_exists($final_destination)){
 $final_destination = $upload.'/'.time().$name;
 }

 if(!move_uploaded_file($tmp_location, $final_destination)){
die("Cannot finish upload, something went wrong");
}

?>

<h2>File Successfully uploaded!</h2>
4

2 回答 2

0

您必须通过以下检查来包装处理已发布文件的代码:

if ( $_REQUEST['METHOD'] == 'POST' && isset($_FILES['file_upload']) ) {

    $file = $_FILES['file_upload'];
    $name = $file['name'];
    $type = $file['type'];
    $tmp_location = $file['tmp_name'];
    $upload = 'uploads';
    $final_destination = $upload.'/'.$name;
    $error = $file['error'];
    $max_upload_size = 2097152;
    $size = $file['size'];

    // ...

}
于 2014-03-18T13:14:32.170 回答
0

您的服务器端代码试图在页面加载时引用提交的表单数据,然后再提交任何内容。您需要将表单处理程序分隔到不同的页面或在引用之前检查表单数据是否存在:

if (isset($_FILES['file_upload'])) {
    // your form-handling code
}
于 2014-03-18T13:15:22.173 回答