snirali
snirali

Reputation: 139

upload form with image in php

I am trying to upload a form with image in it. I am able to get data like brand name and others. Here is my snippet of code. I am not getting image name and type of that. What are the changes require in this code? getting error.

move_uploaded_file(): Unable to move '/tmp/phpRBnjTS' to '/var/www/html/download.jpg' in /var/www/html/rtc/view/setup_config.php on line 156, referer: 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);
    }   
}
?>

Upvotes: 1

Views: 345

Answers (5)

AAT
AAT

Reputation: 411

Please use the below code

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

Upvotes: 2

Ravi Hirani
Ravi Hirani

Reputation: 6539

You have missed enctype="multipart/form-data attribute in your form tag.

For your error, Use below code

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

Hope it will help you :)

Upvotes: 0

Max
Max

Reputation: 891

you should check the manual : http://php.net/manual/en/features.file-upload.post-method.php

Actually you're missing an attribute in the form :

<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>

Upvotes: 1

user5570620
user5570620

Reputation:

You need to add enctype in your form tag

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

Write your form tag as follow:

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

Upvotes: 1

Tarunn
Tarunn

Reputation: 1048

You need to add this in form tag

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

Images need form type to be of encoding multipart.

Upvotes: 2

Related Questions