Muhammad Hafizh
Muhammad Hafizh

Reputation: 35

Upload file trouble in PHP

I have made an upload image a couple times before and I never have problems. And I just copy the script and it worked fine, but after I copy I don't know why it keep shows that I'm not using the right extension. Can you spot the mistake?

This is the form script:

<tr height="30">
        <td>&nbsp;</td>
        <td align="right">Gambar Barang &nbsp;&nbsp;&nbsp;</td>
        <td><input type="file" name="gambar" /></td>
</tr>

and its the action,

$errors= array();
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['gambar']['name']);
$gambar      = $_FILES['gambar']['name'];
$gambar_size     = $_FILES['gambar']['size'];
$gambar_type     = $_FILES['gambar']['type'];
$gambar_tmp      = $_FILES['gambar']['tmp_name'];
$gambar_ext=strtolower(end(explode('.',$_FILES['gambar']['name'])));

 $expensions= array("jpeg","jpg","png");

  if(in_array($gambar_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($gambar_size > 4097152){
     $errors[]='File size must be excately 4 MB';
  }

  if(empty($errors)==true){
     copy($_FILES['gambar']['tmp_name'], $uploadfile);

  }else{
     print_r($errors);
  }

Upvotes: 0

Views: 41

Answers (2)

Reto
Reto

Reputation: 1343

There are several things that could cause file upload to fail:

  • does the upload form have enctype="multipart/form-data"?
  • is file_uploads = On in php.ini?
  • what is the max post/upload limit in php.ini?
  • did you check the permissions for the upload folder?

Upvotes: 2

lovelace
lovelace

Reputation: 1215

Your code works fine, just make sure your form uses:

method='post'

and(as already mentioned by Reto) - enctype="multipart/form-data"

i.e. something along the lines of:

<form method='post' action='#' enctype='multipart/form-data'>

Upvotes: 1

Related Questions