douyingtai6662 2015-11-27 10:54
浏览 47
已采纳

在PHP中显示上传的图像

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

  • 写回答

1条回答 默认 最新

  • dsfjk44656 2015-11-27 21:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)