i am trying to make a page for me to upload files to with a tile i cant get it to show up on the website
if (isset($_POST["submit"]))
{
$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 fileup(title,image) VALUES('$title','$pname')";
if(mysqli_query($conn,$sql)){
echo "File Sucessfully uploaded";
}
else{
echo "Error";
}
}
In here I save the path of the image into the DB.Then retrieve the image source vai DB.
for photo uploading .
$folder ="../uploads/";
$destFile = $folder . basename($_FILES["photo"]["name"]);
$sourdeFile = $_FILES["photo"]["tmp_name"];
if(move_uploaded_file($sourdeFile,$destFile)){
echo "File has been uploaded";
$photo = basename($_FILES["photo"]["name"]);
}else{
echo $_FILES['photo']['error'];
$photo = "images/default.png";
}
$sql= "INSERT INTO car(photo) VALUES('$photo') ";
if(mysqli_query($con,$sql)){
echo
"
<script>
alert ('Your Image successfully upladed');
window.location='newindex.php';
</script>
";
}else{
echo "Error:".mysqli_error($con);
}
To view uploaded images
$sql=" SELECT * FROM car";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result);
<img src="Your default path name/<?=$row["photo"];?>" class="img-thumbnail" style="max-width:400px;max-height:350px;"/>
In here photo will be uploaded into a folder named uploads. At the begoining of the upload name of the image will be save in to DB table . Then to view uploaded images img src
will use the default path name with photo name at the DB. hope you can grab something from this. Also please note that this is only a example. When using SQL
please use parameterized query to avoid SQL injections
. Thanks