This is my first post, so please tell me if theres anything that I have done wrong with this post.
I am currently doing a school task with three subtasks.
1) Make an HTML form with the possibility to upload an image 2) Save the data from the HTML form in a text document and place the image on the server 3) Make a page where the different entries shows up (including the image).
Heres is what I have done so far. Task 1 should be pretty correct.
<!DOCTYPE html>
<html lang="da">
<html>
<head>
<title>Mobiloversigten</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="stylesheet.css">
</head>
<body>
<form name="formular" id="formular" method="post" action="process.php" enctype="multipart/form-data" autocomplete="on">
<fieldset>
<legend>Enhedsoplysninger</legend>
<div><label>Producent: <input type="text" name="manufactor" id="manufactor" required="required" size="30"></label></div>
<div><label>Produktnavn: <input type="text" name="product" id="product" required="required" size="30"></label></div>
<div><label>Farve: <input type="text" name="color" id="color" required="required" size="30"></label></div>
<label>Hukommelse:
<div>
<select size="1" name="memory" id="memory">
<option>8GB</option>
<option>16GB</option>
<option>32GB</option>
<option>64GB</option>
<option>128GB</option>
</select>
</div>
</label>
<div><label>Skærmstørrelse <input type="number" name="displaysize" id="displaysize" min="0" step="0.1" size="30"></label></div>
<div><label>Produktbillede: <input type="file" name="productphoto" id="productphoto" required="required" size="30"></label></div>
</fieldset>
<div><input type="submit" id="ok" value="Indsend"><input type="reset" id="nulstil" value="Nulstil"></div>
</form>
</body>
</html>
My process.php page looks like this:
<!DOCTYPE html>
<html lang="da">
<html>
<head>
<title>Mobiloversigten</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="stylesheet.css">
</head>
<body>
<a href="index.html">Tilføj flere enheder</a>
<a href="overview.php">Vis enheder</a>
<?php
$manufactor = $_POST['manufactor'];
$product = $_POST['product'];
$color = $_POST['color'];
$memory = $_POST['memory'];
$displaysize = $_POST['displaysize'];
$productphoto = $_FILES['productphoto']['name'];
if($_FILES){
if(strlen($_FILES['productphoto']['type'])==9){
move_uploaded_file($_FILES['productphoto']['tmp_name'], $product . ".".$rest = substr($_FILES['productphoto']['type'], -3));
}
else{move_uploaded_file($_FILES['productphoto']['tmp_name'], $product . ".".$rest = substr($_FILES['productphoto']['type'], -4));
}
}
$user_data = "$manufactor, $product, $color, $memory, $displaysize, $productphoto
";
$mobile_data = "Producent: ,Produktnavn: ,Farve: ,Hukommelse: ,Skærmstørrelse: ";
$fh = fopen("data.txt", "a+") or die("Filen kunne ikke oprettes");
fwrite($fh, $user_data) or die("Kunne ikke skrive til filen");
fclose($fh);
$fj = fopen("mobile.txt", "w") or die("Filen kunne ikke oprettes");
fwrite($fj, $mobile_data) or die("Kunne ikke skrive til filen");
fclose($fj);
?>
</body>
</html>
The page which shows the overview of all entries looks like this:
<!DOCTYPE html>
<html lang="da">
<html>
<head>
<title>Mobiloversigt</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Tilføjede enheder</h1>
<?php
fopen("file.txt", "r");
fopen("mobil.txt", "r");
$info = explode(",", file_get_contents("mobil.txt"));
$retrieved_string = file_get_contents("file.txt");
$retrieved_array = explode("
", $retrieved_string);
$count=count($retrieved_array)-1;
for($y = 0; $y < $count; $y++){
$user_data_array = explode(",",$retrieved_array[$y]);
for($x = 0; $x <= 4; $x++){
echo "<p>$info[$x]</p><p2>$user_data_array[$x]</p2><br>";
} echo "<img src=\"$user_data_array[5]\"><br><br>";
echo "<div><span class=\"linie\"></span></div><br><br>";
}
?>
</body>
</html>
The overall problem is, that the pictures don't show up. I see them in my root folder with the .php files and the index. file, but my browser just shows a blue question mark (Safari), so it seems like it doesn't recognize the image location, even though I can see it in the folder.
I would really appreciate some help! :-)
Thanks in advance
/Chris
in process.php the main error is that you are saving the photo with different name and printing tmp_name is file. In acutal both should be same required. Change the code little bit
if($_FILES){
if(strlen($_FILES['productphoto']['type'])==9)
$name=$product . ".".$rest = substr($_FILES['productphoto']['type'], -3);
else
$name=$product . ".".$rest = substr($_FILES['productphoto']['type'], -4);
move_uploaded_file($_FILES['productphoto']['tmp_name'], $name);
}
$user_data = "$manufactor, $product, $color, $memory, $displaysize, $name
";
In overview.php, fopen is not required if you are using file_get_contents thus delete the fopen line and second error correct the input file names as you are saving data in data.txt and mobile.txt but importing data from mobil.txt and file.txt. else every thing is correct.