使用PHP和表单上传图像

时间:2016-03-31 13:04:43

标签: php image forms web get

我在使用表单上传图片并将图片显示在另一页上时出现问题,基本问题是我无法在保存图片的路径上找到图片。

这是form.php

            <form role="form"  id="equipe" action="editEquipe.php" method="get">
              <div class="box-body">




                <div class="form-group">
                  <label for="id_equipe">id Equipe</label>
                  <input type="text" class="form-control" id="id_equipe"  name="id_equipe">
                </div>


               <div class="form-group">
                  <label for="drapeau">Télécharger le logo</label>


                  <input type="file" id="logo"   name="logo">

                </div>

              </div><!-- /.box-body -->

              <div class="box-footer">
                <button type="submit"  value="" class="btn btn-primary">Modifier</button>
              </div>
            </form>

这是我的action.php

if(isset($_GET['id_equipe'])  &&  isset($_GET['logo']))

   {
       $id_equipe= $_GET['id_equipe'];

       $logo =  $_GET['logo'];



    $imgname = "logo_".$id_equipe;
  $logoImage=$imgname.".png";
   $link = "http://.../LogoImages/".$imgname.".png";


if ($conn->connect_error) {
$response["error"] = -1;
$response["message"] = "cannot connect to the server please try          again";
  }


 else
    {

    $sql = "UPDATE equipe set
    nomEquipe='$nom_equipe',
    logoEquipe=' $link',
    idChampionnat='$nom_championnat'

    where idEquipe='$id_equipe' ";

    $updatedispo = $conn->query(sprintf($sql));
    if($updatedispo)
    {           


     $imsrc = base64_decode($logoImage);


     $path ="http://.../LogoImages/".$imgname.".png";

     $path = str_replace('\/','/',$path);




    $fp = fopen($path, 'wb');
    var_dump($fp);


    if(is_readable($fp)) echo 'readable ';
    else echo "no redable ";

    if(is_writable($fp)) echo 'writable ';
        else echo "no writable";

    fwrite($fp, $logoImage);
     // var_dump($fp);

      fclose($fp);



                     $response["error"] = 0;
                     $response['picture'] = $path;

                     $response['message'] =  "Image uploaded in ".$path;
                     $response['id_image'] = $id_equipe;
                     $response["message"] = " equipe updated";


    }

    else
    {
                    $response["error"] = -1;
                    $response["message"] = "error requete"; 
    }
   }
    }


   else
      {
         $response["error"] = 100;
         $response["message"] = "Some params are missing";   
       }
     echo json_encode($response);


        ?>

2 个答案:

答案 0 :(得分:0)

如前所述,您必须使用POST作为方法。然后可以在$ _FILE varibale中找到上传图像的路径。

$tmp_path = $_FILES['logo']['tmp_name'];
$filesize = $_FILES['logo']['size'];
//filesize is bigger then 0 if the logo was set

您可以将文件移动到www文件夹(http://php.net/manual/en/function.move-uploaded-file.php),,但最好检查一下,如果此文件有效!,但这不是问题。

答案 1 :(得分:0)

以形式enctype =“multipart / form-data”添加此项 像

<form role="form" id="equipe" enctype="multipart/form-data" action="editEquipe.php" method="get">

这将用于图片上传。