收到此错误我该如何解决?我只需要指向正确的方向
注意:第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>
答案 0 :(得分: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'];
// ...
}
答案 1 :(得分:0)
您的服务器端代码在页面加载之前尝试引用提交的表单数据,然后再提交任何内容。您需要将表单处理程序分隔到不同的页面,或者在引用表单数据之前检查表单数据是否存在:
if (isset($_FILES['file_upload'])) {
// your form-handling code
}