Question

I'm having an issue with uploading an image path into a database. Database called: basketball_database and table called: media, columns: id(int, auto increment), name(varchar), image(varchar)

All the images I try to upload from different directories on my computers are png files. For some reason I'm only able to upload only 1 image and the database doesn't display the image name or the image path. Can anyone help me fix these problems? Thank you

File 1: upload_image.php

<html>
    <body>
    <form action="uploaded_file.php" method="post"
    enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>

File 2:upload_file.php

//connection
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "media";

$con = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to Mysql");
// echo "Connected to mysql<br>";

 mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

//image extensions allowed
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    { 

    //Successfully uploaded
    echo "Your file " . $_FILES["file"]["name"] . " successfully uploaded!!<br>";
    echo "Details :";    
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

    //Display Image
    echo "<img src=uploaded/" . $_FILES["file"]["name"] . ">";

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

      }
    }
  }
else
  {
    //error message on the extension that are not allowed
    echo "Invalid file"; 
    $filename = preg_replace('/[^A-Z0-9]/','',$_FILES["file"]["name"]) . $extension;
    $logo = uploaded/$filename; 

    //insert into database
        $strSQL = "INSERT INTO $table(name,image) VALUES('$name_file','$logo')";
    mysql_query($strSQL) or die(mysql_error());
  }
?>
Was it helpful?

Solution

Here is a good tutorial to start with upload img with php. And just build up from here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top