【问题标题】:Failed to show the result无法显示结果
【发布时间】:2015-11-16 17:04:44
【问题描述】:

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

“file”是我的文件上传字段的名称。像这样的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 页面中,它总是执行回显“图像上传”。我尝试不选择文件并单击上传,但我还是一样。如何查找是否在 html 页面中选择并上传了文件。为什么我收到同样的信息。它没有执行 else 块。

【问题讨论】:

  • 您的 html 代码中缺少很多引号。你想确保它被大多数浏览器呈现。
  • 好的,谢谢,请回答我的问题

标签: php mysql server


【解决方案1】:

格式化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
}

【讨论】:

    【解决方案2】:

    将你的 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;
    }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2018-08-25
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多