I am having a problem with using this php script to store an uploaded image file name in a database and store the file with a random generated name in a folder.
I get error show up on my sceen as a result of my script producing an echo however no real errors show up. The file is being stored in my directory folder however it doesn't have a random name given to it and it is not being stored in the database. I have spent an hour trying to figure what could be wrong. I would seriously appreciate help.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<label>Title</label>
<input type="text" name="title">
<label>File Upload</label>
<input type="File" name="file">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
$localhost = "localhost"; #localhost
$dbusername = "root"; #username of phpmyadmin
$dbpassword = " "; #password of phpmyadmin
$dbname = "fun"; #database name
#connection string
$conn = mysqli_connect($localhost,$dbusername,$dbpassword,$dbname);
if (isset($_POST["submit"]))
{
#retrieve file title
$title = $_POST["title"];
#file name with a random number so that similar dont get replaced
$pname = rand(1000,10000)."-".$_FILES["file"]["name"];
#temporary file name to store file
$tname = $_FILES["file"]["tmp_name"];
#upload directory path
$uploads_dir = "images/";
#TO move the uploaded file to specific location
move_uploaded_file($tname, $uploads_dir.'/'.$pname);
#sql query to insert into database
$sql = "INSERT into fun(image) VALUES('$pname')";
if(mysqli_query($conn,$sql)){
echo "File Sucessfully uploaded";
}
else{
echo "Error";
}
}
?>