soundy
soundy

Reputation: 307

How to upload file into server using smarty?

I've written code for upload file to server like below.

Home.tpl

     <form enctype="multipart/form-data" method="post" action="upload_file.php"  >
     <label for="file">Filename:</label>&nbsp;&nbsp;
     <input type="file" name="file" id="file"><br>

upload_file.php

     if(($_FILES["file"]["size"] > 0))
     {
      $fileName = $_FILES["file"]["name"];//the files name takes from the HTML form
      $fileTmpLoc = $_FILES["file"]["tmp_name"];//file in the PHP tmp folder
      $fileType = $_FILES["file"]["type"];//the type of file 
      $fileSize = $_FILES["file"]["size"];//file size in bytes
      $fileErrorMsg = $_FILES["file"]["error"];//0 for false and 1 for true
      $target_path = "uploads/" . basename( $_FILES["file"]["name"]); 

      $moveResult = move_uploaded_file($fileTmpLoc, $target_path);
     }

But i getting 'Undefined index: file' error. please help me to get rid from there.

Upvotes: 0

Views: 4161

Answers (3)

Andi
Andi

Reputation: 11

i think its $_FILES["file"]["tmp_name"][0] for the first file. check it with var_dump($_FILES); or print_r($_FILES) then iterate throu it with an foreach. something like

foreach ($arr as &$value) {
    //...
    $moveResult = move_uploaded_file($fileTmpLoc, "uploads/" . basename( $_FILES["file"]["name"][$arr]); 
}

Upvotes: 0

Martin Perry
Martin Perry

Reputation: 9527

If you are getting error every time, its because you dont have set variable $_FILES. That variable is set only after you submit your form.

In that case, to get rid of error mesage, add control of variable:

if((isset($_FILES["file"])) && ($_FILES["file"]["size"] > 0))

Upvotes: 1

Nauphal
Nauphal

Reputation: 6202

change

if(($_FILES["file"]["size"] > 0))

to

if((isset($_FILES["file"]["size"]) && $_FILES["file"]["size"] > 0))

Upvotes: 0

Related Questions