我想制作一个简单的图片上传器。
我有两页' index.html'和' upload.php'
以下是index.html的代码
<html>
<head>
<script>
$(document).ready(function(){
$("#im").click(function() {
$("input[id='my_file']").click();
});
$("#su").click(function() {
$("input[id='submit']").click();
});
});
</script>
</head>
<body>
<form method="post" action="upload.php" >
<img id="im" src="pic.png" width="30" height="30" />
<img id="su" src="sent.png" width="50" height="50" />
<input type="file" id="my_file" style="display: none;" name="img" />
<input type="submit" id="submit" style="display: none;" value="Upload Image" name="submit"/>
</form>
以下是upload.php的代码:
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['img']['name']);
if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['img']['name']).
" has been uploaded";
} else{
echo "fail : ". $_FILES['img']['error'] ;
}
?>
我也创建了dir上传,我检查了它是否可写,确实如此。 我编辑了php.ini并启用了上传并将最大大小提高到50米
当我上传文件时,它会显示:&#34;失败:&#34;
答案 0 :(得分:5)
如果您有用于上传文件的表单,则需要将enctype='multipart/form-data'
属性添加到表单。
<form method="post" action="upload.php" enctype="multipart/form-data">
答案 1 :(得分:0)
尝试使用这个:PHP代码
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>