Jump to content

Recommended Posts

I am still really new to php so I am sorry if I don't make much sense but what I want to be able to achieve is to retrieve images from a file directory that will be referenced in the database.

 

Here is my code for submitting the images

 

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
 {
 if ($_FILES["file"]["error"] > 0)
   {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
 else
   {
   echo "Upload: " . $_FILES["file"]["name"] . "<br>";
   echo "Type: " . $_FILES["file"]["type"] . "<br>";
   echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
   echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

   if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
   else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
   }
 }
else
 {
 echo "Invalid file";
 }
?>

 

Here is my sql for the created table

 

CREATE TABLE `recipes` (
   `recipeid` INT(11) UNSIGNED ZEROFILL PRIMARY KEY AUTO_INCREMENT,
   `recipename` VARCHAR(50) NOT NULL,
   `ingredients` VARCHAR(50) NOT NULL,
   `instructions` VARCHAR(50) NOT NULL,
   `imagename` VARCHAR(50) NOT NULL,


   `created` DATETIME NOT NULL
)

 

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/274179-referencing-images-in-the-database/
Share on other sites

That is the upload script, so I have managed to upload the photo to the server but I want to retrieve it.

 

This code might make more sense.

 

<!DOCTYPE html>
<html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <head><link rel="stylesheet" type="text/css" href="../style/user.css"></head>
    <title>Users</title>
   </head>
<?php
include('common.php');

   $sqlget ="SELECT * FROM recipes";

   $sqldata= mysqli_query($dbcon, $sqlget) or die('error getting data');
?>

   <body>
   <section id="mainContainer">
      <h1>test</h1>
           <section id="photo">
           <h1>test</h1>
           <a href="#">Edit Photo</a>
           </section>
           <section id="ad1">
           <h1>test</h1>
           </section>
           <section id="leftContainer">
           <h1>Saved Recipe</h1>
           <input type="Submit" Value="View more recipes"/>
           </section>
           <section id="ad2">
           <h1>test</h1>
           </section>
           <section id="middleContainer">
               <?php

   echo "<table>";
   echo "<tr><th>ID</th><th>Recipe Name</th><th>Ingredients</th><th>Instructions</th></tr>";

   while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {    
       echo "<tr><td>";
       echo $row['recipeid'];
       echo "</td><td>";
       echo $row['recipename'];
       echo "</td><td>";
       echo $row['ingredients'];
       echo "</td><td>";
       echo $row['instructions'];
       echo "</td></tr>";
       }
       echo "</table>";
   ?>
           <h1>About Me</h1>
           </section>


      </section>
   </body>
</html>

 

I basically want each recipe to display an image

Oh ok. Yes, you'll need them in the database first, before you display them :happy-04:

 

Depending on if the recipe is already in the database or not, you'll need to either UPDATE or INSERT. Is the recipe already in the database when submitting the image?

Hopefully this helps:

 



$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
   if ($_FILES["file"]["error"] > 0)
   {
       echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
   else
   {
       echo "Upload: " . $_FILES["file"]["name"] . "<br>";
       echo "Type: " . $_FILES["file"]["type"] . "<br>";
       echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
       echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

       if (file_exists("upload/" . $_FILES["file"]["name"]))
       {
           echo $_FILES["file"]["name"] . " already exists. ";
       }
       else
       {
           move_uploaded_file($_FILES["file"]["tmp_name"],
           "upload/" . $_FILES["file"]["name"]);

           // Connect to database
           include('common.php'); // Assuming it's here? ($dbcon)

           $sql = "UPDATE recipes SET imagename = '{$_FILES['file']['name']}' WHERE recipeid= '{$_GET['recipeid']}'"; // Assuming $_GET is set like this?
           mysqli_query($dbcon, $sql) or trigger_error(mysql_error());

           echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
       }
   }
}
else
{
   echo "Invalid file";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.