无法显示结果

时间:2015-08-23 02:19:26

标签: php mysql server

对于我的项目,我正在使用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>

&#34;文件&#34;是我的文件上传字段的名称。像这样的PHP代码

 <?php

$file=$_FILES['file'];
var_dump($file);
if(isset($_FILES['file']['name']))
{
echo "Image Uploaded";
echo $file['name'];
}
else 
echo "Image not Uploaded";
?>

无论文件是上传还是未上传,无论是在我的PHP页面中,它总是执行echo&#34;图像上传&#34;。我尝试没有选择文件,然后点击上传仍然是我得到相同。如何查找是否在html页面中选择并上载了文件。为什么我得到同样的信息。它没有执行else块。

2 个答案:

答案 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>

Php检查文件是否被选中

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

答案 1 :(得分: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;
}
}
?>