如何在php中上传目录和数据库中的图像

时间:2018-01-10 05:55:10

标签: php

我想将imagename上传到数据库和目录,然后想要在预览页面上显示它(preview.php)。但它根本不起作用。因为我是编程新手,所以我需要支持。这里的问题是,图像既不会保存到目录中,也不会保存到数据库中。

这里是表单页面(form5.php),我们选择图像,点击提交后转到下一页pptupload.php。

<form method="post" action="pptupload.php?appid=<?php echo $appid; ?>" enctype='multipart/form-data'>

                            <table>
                              <tbody>
                                  <tr>
                                      <img id="pptimg" src="#" width="600px" height="300px" alt="Scanned Passport" />   
                                      <input type="file" id="file" name="file" />
                                   </tr>

                                      <tr>
                                         <input name='but_upload' type="submit" class="btn btn-primary" value="Save and Continue">
                                      </tr>
                        </tbody>
                     </table>
                </form>

这是下一页(pptupload.php)。在此页面上,我想在成功上传图像后重定向到下一页preview.php,如果失败则显示错误并返回到名为form5.php的图像选择页面。

<?php

session_start();

$appid = $_GET['appid'];

include("connect.php");

if(isset($_POST['upload'])){

 $name = $_FILES['file']['name'];
 $target_dir = "upload/";
 $target_file = $target_dir . basename($_FILES["file"]["name"]);

 // Select file type
 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

 // Valid file extensions
 $extensions_arr = array("jpg","jpeg","png","gif");

 // Check extension
 if( in_array($imageFileType,$extensions_arr) ){

  // Upload file
  move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

 }

}

// Insert record
  $query = "insert into payments(app_id, pptimg) values('$appid','$name')";
  if (mysqli_query($connect,$query)){
    echo "Image were updated successfully.";
    header("Location: preview.php?appid=".$appid);
    }else{
    echo "Photo not uploaded".mysqli_error($connect);
    }

?>

1 个答案:

答案 0 :(得分:0)

检查以下代码:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>