user225269
user225269

Reputation: 10913

How to compare filename of uploaded file and string

I use this code to upload image files in xammp server:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
  {
  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, File must be less than 100Kb in size with .jpg, .jpeg, or .gif file extension";
  }
?>

What do I do to compare the file name of the uploaded files with the text inputted by the user? My goal is to be able to compare the user input(ID number) and the file name of the image file which should also be an ID number. So that I will be able to display the image that corresponds with the ID Number provided. What do I need to do?Please give me an idea on how can I achieve this. Thanks

Upvotes: 1

Views: 1760

Answers (1)

Nullw0rm
Nullw0rm

Reputation: 913

You're not making much sense, Are you wanting to compare the ID number the user enters to the uploaded file?

$idnumber = $_POST['id']; //User inputted

if($_FILES["file"]["name"]) != "$idnumber" {
  exit("Does not match");
}

Or if you're wanting to match files en-mass you should create a database entry for each image path alongside their image ID.

If you're wanting to display the image of the ID number:

$idnumber = $_POST['id']; //User inputted

print "<img src='/images/$idnumber.ext'/>";

Upvotes: 2

Related Questions