我试图上传图片文件,我想显示该图片的信息。但我的代码不起作用,我不确定是什么问题,我希望有人可以指出我做错了什么。
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload_image.php" action="post" enctype="multipart/form-data">
Select image: <input type="file" name="image">
<input type="submit" name="upload" value="Upload Now">
</form>
<?php
if(isset($_POST['upload'])){
echo $image_name = $_FILES['image']['name'];
echo $image_type = $_FILES['image']['type'];
echo $image_size = $_FILES['image']['size'];
echo $image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name == ""){
echo "<script>alert('Please select an image!')</script>";
exit();
}
}
?>
</body>
</html>
ps:我还是php和文件上传的新手,所以请放轻松我...提前谢谢你
答案 0 :(得分:1)
问题似乎在于您没有指定上传位置的任何位置,也没有指定文件类型。这是带有扩展验证,大小和将文件移动到名为images的文件夹中的代码。
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file."
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>