【问题标题】:Upload image using PHP and forms使用 PHP 和表单上传图片
【发布时间】:2016-03-31 13:04:43
【问题描述】:

我在使用表单上传图片并在另一个页面上显示时遇到问题,基本问题是我在保存它的路径上找不到图片。

这是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);


        ?>

【问题讨论】:

标签: php image forms web get


【解决方案1】:

将其添加到表单 enctype="multipart/form-data"
喜欢

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

这将用于图片上传。

【讨论】:

  • 现在我不能下载图片但是显示它的权限总是600,我希望权限访问将777作为默认值,我该怎么做????任何想法,我正在使用 FTP
【解决方案2】:

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

$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),但最好检查一下,此文件是否有效!,但这不是问题所在。

【讨论】:

    猜你喜欢
    • 2014-05-19
    • 1970-01-01
    • 2019-05-05
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多