SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error only on .gif upload

    With images I don't have problem.. only with .gifs. Here is the code that I use
    Code:
    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';
    }
    The error that I get is Error 2: ERROR upload file.
    var_dump($_FILES) result is
    Code:
    array (size=1)
      'userfile' => 
        array (size=5)
          'name' => string 'azbRWYK460sa.gif' (length=16)
          'type' => string '' (length=0)
          'tmp_name' => string '' (length=0)
          'error' => int 1
          'size' => int 0
    Something is wrong but I can't figured it what.

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    726
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    You've not posted your form but I the error is saying the file is larger than defined in the form. Be sure to use commas like so.
    Code:
    <input type="hidden" name="MAX_FILE_SIZE" value="20,000,000">
    If you get past error 2 and it's in your extension check you could always change your array like below and compare to $ext.

    PHP Code:
    $permitted = array('jpeg''jpg''png''gif'); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank's for your answer.

    The problem was that I found second php.ini file where I have limits. I've changed them and now seems to work.

    I have no idea why there is two php.ini files. One in 'Bin/Apache' folder and one in 'Bin/php' folder of WAMP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •