繁体   English   中英

如何在 php 中显示图像?

[英]How do I display images in php?

我无法在 php 中显示图像。 在第一个文件中,我有一个获取图像的输入。 <input type="file" name="imageOrVideo" accept="video/*,image/*" multiple> 在第二个 php 文件中,我想获取图像并显示它。 我使用$imageOrVideo = $_POST['imageOrVideo'];获取文件如果我回显 $imageOrVideo,我得到的只是一个字符串。 我尝试使用 echo img echo "<img src=Image/".$imageOrVideo."/>"; echo "<img src=".$imageOrVideo."/>"; 但都没有奏效。 我读到您只能回显字符串,所以也许我不应该使用回显,而是使用其他方法来显示图像。 这是完整的代码,以便查看问题可能出在哪里。 谢谢你的帮助。 这是第一个文件。

<!DOCTYPE html>
<html>
<head>
    <title>Create Recipe</title>
    <link rel="stylesheet" type="text/css" href="../CSS/createRecipe.css">
</head>
<body>

<div class="topnav">
    <a href="includes/logout.inc.php">Sign Out</a>
    <a href="">My Recipes</a>
    <a href="index.php">Home</a>
</div>

<form action="includes/createRecipe.inc.php" method="post">
<div class="leftSide">

<input type="file" name="imageOrVideo" accept="video/*,image/*" multiple><br><br>
<!--
 <input type="file" name="imageOrVideo" accept="image/*" ><br><br>
 --> 


<div class="prepTimeCookTime">
<label>Prep Time</label><br><br>
<textarea type="" name="prepTime" class="prepTime"> </textarea><br>

<label>Cook Time</label><br><br>
<textarea type="" name="cookTime" class="cookTime"> </textarea><br>
</div>

<div class="readyTimeNumberOfServings">
<label></label>Ready Time<br><br>
<textarea type="" name="readyTime" class="readyTime"> </textarea><br>

<label>Number of Servings</label><br><br>
<textarea type="" name="numberOfServings" class="numberOfServings"> </textarea><br>
</div>
</div>

<div class="rightSide">
<label>Recipe Title</label><br><br>
<textarea type="" name="recipeTitle" class="recipeTitle"> </textarea><br>

<label>Description</label><br><br>
<textarea type="" name="description" class="description"> </textarea><br>

<label>Ingrediants</label><br><br>
<textarea type="" name="ingredients" class="ingredients" placeholder="Put each ingrediant on its own line."></textarea><br>

<label>Directions</label><br><br>
<textarea type="" name="directions" class="directions" placeholder="Put each step on its own line."></textarea><br>

<button type="submit" name="recipe-submit">Submit</button>
</div>
</form>

</body>
</html>

这是第二个文件。

<?php
    if(isset($_POST['recipe-submit'])){

        require 'dbh.inc.php';
        session_start();
        

        $imageOrVideo = $_POST['imageOrVideo'];
        $uidUsers = $_SESSION['userUid'];
        $prepTime = $_POST['prepTime'];
        $cookTime = $_POST['cookTime'];
        $readyTime = $_POST['readyTime'];
        $numberOfServings = $_POST['numberOfServings'];
        $recipeTitle = $_POST['recipeTitle'];
        $description = $_POST['description'];
        $ingredients = $_POST['ingredients'];
        $directions = $_POST['directions'];
        
        //echo "<img src='" . $_POST['imageOrVideo'] . "' alt='img'>";
        //echo "<img src='/images/test/" . $_POST['imageOrVideo'] . "' alt='img'>";
        //print $imageOrVideo;
        //echo  "<img src=".$imageOrVideo."/>";
        //echo "<img src=Image/".$imageOrVideo."/>";


        

/*
        $sql = "INSERT INTO recipes (videoOrImages, uidUsers, prepTime, cookTime, readyTime, numberOfServings, recipeTitle, description, ingredients, directions) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    $stmt = mysqli_stmt_init($conn);
                    if(!mysqli_stmt_prepare($stmt, $sql)){
                        header("Location: ../createRecipe.php?error=sqlError");
                        exit();
                    }
                    else{
                        mysqli_stmt_bind_param($stmt, "bsssssssss", $imageOrVideo, $uidUsers, $prepTime, $cookTime, $readyTime, $numberOfServings, $recipeTitle, $description, $ingredients, $directions);
                        mysqli_stmt_execute($stmt);
                        header("Location: ../index.php?signup=success");
                        exit();
                    }   
                
                    

            mysqli_stmt_close($stmt);
            mysqli_close($conn);
            
            */
    }

    else{
        header("Location: ../createRecipe.php");
        exit();
    }

        

正如我在评论中提到的,您实际上并未管理上传的文件。 尝试这样的事情:

    $uidUsers = $_SESSION['userUid'];
    $prepTime = $_POST['prepTime'];
    $cookTime = $_POST['cookTime'];
    $readyTime = $_POST['readyTime'];
    $numberOfServings = $_POST['numberOfServings'];
    $recipeTitle = $_POST['recipeTitle'];
    $description = $_POST['description'];
    $ingredients = $_POST['ingredients'];
    $directions = $_POST['directions'];
    $imageOrVideo = $_POST['imageOrVideo'];

    $uploaddir = 'uploads/'; //or wherever you want to upload the file
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    $imageuploaded = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

   // then check to see if its actually an image

   $check = getimagesize($_FILES["file"]["tmp_name"]);
   if($check !== false) {
   echo "File is an image";
   $imageuploaded = 1;
   } else {
   echo "File is not an image.";
   $imageuploaded = 0;
   }

   // you can check to see if the file already exists
  
   if (file_exists($target_file)) {
   echo "Sorry, file already exists.";
   $imageuploaded = 0;
   }

   // then process the file

  if ($imageuploaded == 0) {
  echo "Sorry, your file was not uploaded.";

  // if everything is ok, try to upload file

  } else {

  if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile)) {
  echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
  } else {
  echo "Sorry, there was an error uploading your file.";
    }
  }

如果你想确保它是一个特定的文件类型,你可以尝试这样的事情:

  // 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;
  }

我还没有测试过代码,但它应该可以工作,或者至少让你走上正确的道路

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM