不能用html / php表格上传图片

时间:2013-11-23 22:23:51

标签: php html css

我正在使用html表单通过php将我的数据保存为xml。

但是我收到此错误消息

注意:未定义的索引:第6行的C:\ xampp \ htdocs \ fyp \ createxml.php中的图片

注意:未定义的索引:第27行的C:\ xampp \ htdocs \ fyp \ createxml.php中的图片

警告:copy():第27行的C:\ xampp \ htdocs \ fyp \ createxml.php中的文件名不能为空 上传图片时出错

这是我的php和html代码

PHP:

<?php
    //Recibe data
if(!empty($_POST))
{
    $name = $_POST['name'];
    $picture = $_FILES['picture'];

    $portionNum = $_POST['portion_num'];


    $prepTime = $_POST['prep_time'];
    $recipeDescrip = $_POST['descrip'];

    $utensilNum = $_POST['utensil_num'];
    $utensilDescrip = $_POST['utensil_descrip'];

    $quantityAmount = $_POST['quantity_amount'];
    $ingredientName = $_POST['ingredient_name'];

    $stepNum = $_POST['step_num'];
    $stepDetail = $_POST['step_detail'];
  //Now you have data you need move the file to one folder or you lose it
  //The picture must move to one folder or you lose the picture before save on xml

  $destiny = '\images';

  if (copy($_FILES['picture']['tmp_name'],$destiny)) 
  {
    //here you create the xml file because the file is seccure on the folder.

    $recipe = array('name'=>$name,
                    'portion_num'=>$portionNum,
                    'prep_time'=>$prepTime,
                    'descrip' => $recipeDescrip,
                    'utensil_num' => $utensilNum,
                    'utensil_descrip' => $utensilDescrip,
                    'quantity_amount' => $quantityAmount,
                    'ingredient_name' => $ingredientName,
                    'step_number' => $stepNum,
                    'stepDetail' => $stepDetail,
                    'picture'=>$destiny."/".$_FILES['picture']['tmp_name']); //This data will be save on the xml
    //on the xml you only save the name for the picture

    $doc = new DOMDocument(); //open the object xml 
    $r = $doc->createElement("recipe"); 
    $doc->appendChild($r); 

    $name = $doc->createElement('name');
    $r->appendChild($name);    

    $adress = $doc->createElement('portion_num');
    $r->appendChild($portionNum);    

    //Finish the xml and save it
    echo $doc->saveXML();
    $doc->save($_POST['name'].".xml");

  } else {
    echo "Error to upload the picture";
   }

}
?>

HTML

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
    <title> Input Recipe </title>
    <meta charset="utf8">
  </head>
  <body>

      <form id="recipe_form" name ="recipe_form" form action="createxml.php" enctype="multipart/form-data" method="post">
        <h1> Recipe Name</h1>
        <input type="text" name="name" placeholder="Name"><br />
        <input type="file" name="picture"><br />
        <br />
        <h1> Portion</h1>
        <input type="text" name="portion_num" placeholder="Portion number"><br />
        <br />
        <h1> Preperation Time + Description</h1>
        <input type="text" name="prep_time" placeholder="Preperation Time"><br />
        <input type="text" name="descrip" placeholder="Recipe Description"><br />
        <br />
           <h1> Untensils </h1>
        <input type="text" name="utensil_num" placeholder="Utensil Number"><br />
        <input type="text" name="utensil_descrip" placeholder="Untensil Description"><br />
        <br />
        <h1> Ingredients </h1>
         <input type="text" name="quantity_amount" placeholder="Quantity Amount"><br />
         <input type="text" name="ingredient_name" placeholder="Ingredient Name"><br />
         <br />
         <h1> Method</h1>
         <input type="text" name="step_num" placeholder="step_number"><br />
         <input type="text" name="step_detail" placeholder="Step Description">

        <br />
        <button type="submit">Save on xml</button>
      </form>

  </body>
</html>

0 个答案:

没有答案