2016-02-12 34 views
1

我试图上传一张带有图像的表单。我能够获取品牌名称等数据。这是我的代码片段。我没有获得图像名称和类型。这段代码需要做什么修改?得到错误用php中的图像上传表格

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); 
    } 
} 
?> 
+1

缺少的属性是enctype = “的multipart/form-data的”,它讲述了照顾浏览器文件上传! –

+0

对于您的错误,请检查文件夹权限。 –

回答

2

你需要在form标签添加这个

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

图像需要形式类型T o是编码多部分。

1

您需要在您的表单标签

的enctype属性指定如何形成的数据应该将它提交到服务器时被编码添加enctype

写您的表单标签如下:

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

你应该检查手册: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> 
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); 
    } 
} 
?> 
+0

它不适合我 – snirali

0

您在表单标记中错过了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); 

希望它会帮助你:)

在你的表单标签