-1

I'm able to upload any image with this code but when I try to upload gif I get an error. Here is how I trying and the error that I get is Error 2: ERROR upload file. This is on the second IF block. What can be wrong here?

define('MAX_FILE_SIZE', 20000000000);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
if (isset($_POST['upload'])) {

$caption = $_POST['caption'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$category = $_POST['gif_cat'];

$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());

// gif name with extension
$myFile = $randName . '.' . $ext;
// save gif path
$path = "../upload/gifs/" . $myFile;

if (in_array($fileType, $permitted) && $fileSize > 0
    && $fileSize <= MAX_FILE_SIZE) {

    $result = move_uploaded_file($tmpName, $path);

    if (!$result) {
        echo "Error uploading gif file";
        exit;
    } else {
        $db = new mysqli("localhost", "user", "pass", "table");

        if (mysqli_connect_errno()) {
            printf("Connect failed: %s<br/>", mysqli_connect_error());
        }
        mysqli_set_charset($db, "UTF8");

        $query = "INSERT INTO gifs (caption, name, size, type, file_path, gif_cat) VALUES (?,?,?,?,?,?)";
        $conn = $db->prepare($query);
        if ($conn == TRUE) {
            $conn->bind_param("ssisss",$caption, $myFile, $fileSize, $fileType, $path, $category);
            if (!$conn->execute()) {
                echo 'error insert';
            } else {
                echo "Gif {$_FILES['userfile']['name']} was successfully uploaded<br />
                <a href='index.php'>Add another gif</a><br />";
                exit;
            }
        } else {
            die("Error 1: ERROR preparing Statement");
        }
    }
} else {
    echo 'Error 2: ERROR upload file';
}
} else {
echo 'Error 3';
}

var_dump($_FILES)

array (size=1)
'userfile' => 
array (size=5)
  'name' => string 'azbRWYK_460sa.gif' (length=17)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0

UPDATE:

This is happen with .gifs bigger than 1MB .. I was able to upload < 1MB gif.

UPDATE 2: That strange. On phpinfo() result is

max_file_uploads    20  20
post_max_size   8M  8M
upload_max_filesize 2M  2M

but in my php.ini I have

upload_max_filesize = 20M
post_max_size = 20M
max_file_uploads - I don't have this in php.ini?!

How I have this differences? And where to find this second php.ini?

UPDATE 3:

Ok, I found another php.ini in apache folder and after I change values now is work. I never thought of second php.ini file.

Goro
  • 499
  • 1
  • 13
  • 31

2 Answers2

2

Actually this part of your if statement is getting failed and this you are taking to the Error 2: ERROR upload file block.

  if (in_array($fileType, $permitted) && $fileSize > 0
        && $fileSize <= MAX_FILE_SIZE) {

Make a var_dump() of the variables $fileType and $fileSize and see if they satisfy your if condition.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • I can't figured out why this part failed since `$filetype` and `$permitted` is ok.. I edited my question and added result from var_dump. – Goro Feb 26 '14 at 07:25
  • 1
    See your `$fileType` is just an empty variable and that's why your code fails. Also you don't need to `var_dump` the `$permitted` variable as we already know it (what is inside of it) through the code. You need to `var_dump` the `$fileSize` actually too. – Shankar Narayana Damodaran Feb 26 '14 at 07:28
  • `var_dump($fileSize)` return 0 `int 0` – Goro Feb 26 '14 at 07:55
  • `0 > 0` Condition still fails. It's better to `var_dump` your `$_FILES` array itself. – Shankar Narayana Damodaran Feb 26 '14 at 08:04
  • updated my question with `var_dump($_FILES)` and still don't know why is this – Goro Feb 26 '14 at 08:11
  • This is happen with .gifs bigger than 1MB .. I was able to upload < 1MB gif. – Goro Feb 26 '14 at 12:27
  • Did you check [**this**](http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size) thread how to set limit ? – Shankar Narayana Damodaran Feb 26 '14 at 12:29
  • Yes, both are 20MB in my php.ini. Strange but I was able to upload just one gif. Every next doesn't matter how big is doesn't upload. And on var_dump I get proper info `'size' => int 487861` and other are ok. I don't know what else can be.. – Goro Feb 26 '14 at 12:34
  • So the problem is with .gif's bigger than 1MB – Goro Feb 26 '14 at 12:37
0

Check your php.ini file and phpinfo() what will return and if they equal. Check if you have more than one php.ini file.

I had the same problem before and it turned out that this is the reason.

Jason Paddle
  • 1,095
  • 1
  • 19
  • 37