【问题标题】:upload form with image in php在php中上传带有图像的表单
【发布时间】:2016-02-12 08:48:12
【问题描述】:

我正在尝试上传包含图片的表单。我能够获得品牌名称等数据。这是我的sn-p代码。我没有得到图像名称和类型。此代码需要进行哪些更改?得到错误

move_uploaded_file(): 无法将 '/tmp/phpRBnjTS' 移动到 '/var/www/html/download.jpg' 在 /var/www/html/rtc/view/setup_config.php 第 156 行,引用者: http://192.168.50.123/rtc/view/setup_config.php

<form name="formcfg" id="formcfg" action="" method="post">
    <input type="hidden" name="mode" id="mode" value="insert" />
    <div id="dashboard">
      <h2>Brand Name</h2>
      <div>
        <table width="100%">
        <tr>
            <td width="15%">Brand Name*</td>
            <td width="92%">
              <input type="text" name="context" id="context" class="input required" placeholder="Brand /directory Name" title="Brand /directory Name"/>
            </td>
        </tr> 

        <tr>
            <td width="15%">Brand Logo</td>
            <td  id="tmp" width="92%">

            <input type ="file" name ="image"  style="width:180px;height:20px"><span id='val'></span>
              <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />

            </td>
        </tr>  
        </table> 
      </div>
    <span class="clear" style="float:left; margin-top:5px;margin-bottom:5px; margin-left:60px;">
    <input type="submit" name="submit" required class="btn"  value="submit"></span>
</form> 
<?php
if(isset($_POST['submit']) || isset($_FILES['image']['name'])) 
{
    $BrandName=$_POST['context'];
    $filename  = $_FILES['image']['name'];
    $type=$_FILES['image']['type'];
    $path= '/var/www/html/';
    $filedata  = file_get_contents($fpath);
    error_log("===file  is $filename===");


    if($filename!="")
    {
        move_uploaded_file($_FILES['image']['tmp_name'],$path.$filename);
    }   
}
?>

【问题讨论】:

  • 表单标签中缺少属性 enctype="multipart/form-data" 告诉浏览器注意文件上传!
  • 对于您的错误,请检查文件夹权限。

标签: php html


【解决方案1】:

你需要在表单标签中添加这个

<form name="formcfg" id="formcfg" action="" method="post" enctype="multipart/form-data">

图像需要表单类型为多部分编码。

【讨论】:

    【解决方案2】:

    请使用以下代码

    <form name="formcfg" id="formcfg" action="" method="post" enctype="multipart/form-data">
        <input type="hidden" name="mode" id="mode" value="insert" />
        <div id="dashboard">
          <h2>Brand Name</h2>
          <div>
            <table width="100%">
            <tr>
                <td width="15%">Brand Name*</td>
                <td width="92%">
                  <input type="text" name="context" id="context" class="input required" placeholder="Brand /directory Name" title="Brand /directory Name"/>
                </td>
            </tr> 
    
            <tr>
                <td width="15%">Brand Logo</td>
                <td  id="tmp" width="92%">
    
                <input type ="file" name ="image"  style="width:180px;height:20px"><span id='val'></span>
                  <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    
                </td>
            </tr>  
            </table> 
          </div>
        <span class="clear" style="float:left; margin-top:5px;margin-bottom:5px; margin-left:60px;">
        <input type="submit" name="submit" required class="btn"  value="submit"></span>
    </form> 
    <?php
    if(isset($_POST['submit'])) 
    { 
        $BrandName=$_POST['context'];
        $filename  = $_FILES['image']['name'];
        $type=$_FILES['image']['type'];
        $path= 'var/www/html/';
        //$filedata  = file_get_contents($filename);
        error_log("===file  is $filename===");
    
    
        if($filename!="")
        {
            move_uploaded_file($_FILES['image']['tmp_name'],$path.$filename);
        }   
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      您需要在表单标签中添加enctype

      enctype 属性指定表单数据在提交到服务器时应如何编码。

      编写您的表单标签如下:

      <form name="formcfg" id="formcfg" action="" method="post" enctype="multipart/form-data">
      

      【讨论】:

        【解决方案4】:

        您应该查看手册:http://php.net/manual/en/features.file-upload.post-method.php

        实际上你在表单中缺少一个属性:

        <form enctype="multipart/form-data" action="_URL_" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
            Send file : <input name="userfile" type="file" />
            <input type="submit" value="Send file" />
        </form>
        

        【讨论】:

          【解决方案5】:

          您在表单标签中遗漏了enctype="multipart/form-data 属性。

          对于您的错误,请使用以下代码

          $destination_path = getcwd().DIRECTORY_SEPARATOR;
          $target_path = $destination_path . basename( $_FILES["image"]["name"]);
          @move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
          

          希望对你有帮助:)

          【讨论】:

            猜你喜欢
            • 2018-09-03
            • 2015-12-15
            • 2016-12-14
            • 1970-01-01
            • 2011-01-03
            • 2014-12-13
            • 2016-04-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多