2015-08-23 64 views
3

对于我的项目我使用PHP和MySql。在那我试图上传一个图像到MySQL数据库。在那我面对一个可怕的错误。我的html代码是这样的无法显示结果

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Learing new things</title> 
<style> 
body 
{ 
margin:4%; 
} 
</style> 
</head> 

<body> 
<form enctype="multipart/form-data" method=post name=imaging action="upload.php"> 
<input type="file" name=file><input type="submit" name=upload value=Upload> 
</form> 
</body> 
</html> 

“文件”是我的文件上传字段的名称。这样的PHP代码

无论文件是上传还是不上传,在我的PHP页面中,它总是执行echo“image upload”。我尝试没有选择一个文件,并点击上传,但我仍然是相同的。我如何找到一个文件是否被选中并上传到html页面。为什么我得到相同的信息。它不执行else块。

+0

您的HTML代码中缺少大量的引号。你想确保它被大多数浏览器渲染。 –

+0

我错过了什么? –

+0

好的谢谢你,请回答我的问题 –

回答

3

格式HTML代码,如果

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Learing new things</title> 
<style> 
body 
{ 
margin:4%; 
} 
</style> 
</head> 

<body> 
<form enctype="multipart/form-data" method="post" name="imaging" action="upload.php"> 
<input type="file" name="file" /><input type="submit" name="upload" value="Upload" /> 
</form>" 
</body> 
</html> 

腓检查文件选择

if (empty($_FILES['file']['name'])) { 
    // No file was selected for upload 
} 
+0

谢谢你,它实际上工作....谢谢你 –

1

修改你的PHP脚本到这一点。

<?php 
$target_dir = "(your target directory)/"; 
$target_file = $target_dir .basename($_FILES["uploadfile"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["upload"])) { 
$check = getimagesize($_FILES["uploadfile"]["tmp_name"]); 
if($check !== false) { 
    echo "File is an image - " . $check["mime"] . "."; 
    $uploadOk = 1; 
} else { 
    echo "File is not an image."; 
    $uploadOk = 0; 
} 
} 
?>