I am learning php and i am facing a problem. I am creating an upload form and when i upload a pic it doesn’t appear automatically on html form.Below i am posting both html form and php code to help me.
This is my html form. Do i have to include the full path so this can work?
<!DOCTYPE html>
<html>
<head>
<style>
table{
border: 1px solid black;
margin: auto;
margin-top: 150px;
width: 280px;
height: 130px;
display: flex;
}
</style>
</head>
<body>
<table>
<form action="upload.php" method="post" enctype="multipart/form-data">
<tr>
<td>Ανεβάστε την φωτογραφία σας</td>
</tr>
<tr>
<td>
<input type="file" name="file" accept="png,jpg,jpeg">
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="upload">
</td>
</tr>
<?php
if (!empty($uploaded_image)) {
echo '<img src="' . $uploaded_image . '" alt="Uploaded Image" style="width: 300px; height: auto;">';
}
?>
</form>
</table>
</body>
</html>
And this is my php script. I think the problem is with php code i wrote on html form cause files have no problem when uploading.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST["submit"])){
$target_dir="upl/";
$target_file=$target_dir.basename($_FILES["file"]["name"]);
$uploadOk=1;
$image=pathinfo($target_file,PATHINFO_EXTENSION);
$check=getimagesize($_FILES["file"]["tmp_name"]);
if($check!==false){
echo"Το αρχείο είναι εικόνα " .$check["mime"]."<br>";
$uploadOk=1;
}else{
echo"Η εικόνα δεν είναι αρχείο<br>";
$uploadOk=0;
}
if(file_exists($target_file)){
echo"Η εικόνα που ανέβασες υπάρχει ήδη<br>";
$uploadOk=0;
}
if($image!=="jpg"&&$image!=="png"&&$image!=="jpeg"&&$image!=="gif"){
echo"Η μορφή της εικόνας που ανέβασες δεν υποστιρίζεται.Μόνο jpg,png,jpeg,gif<br>";
$uploadOk=0;
}
if($_FILES["file"]["size"]>500000){
echo"Το αρχείο είναι πολύ μεγάλο<br>";
$uploadOk=0;
}
if($uploadOk==0){
echo"<span style=color: red;>Το αρχείο δεν μπόρεσει να ανέβει<br>";
}else{
if(move_uploaded_file($_FILES["file"]["tmp_name"],$target_file)){
$uploaded_image = $target_file;
echo"<span style=color: green;>Το αρχείο " . basename($_FILES["file"]["name"])." ανέβηκε με επιτυχία<br>";
}else{
echo"<span style=color: red;>Το αρχείο δεν ανέβηκε";
}
}
}
?>
New contributor
Γεωργιος Παπαδοπουλος is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.