Frage

I have the below code for uploading an image file:

<?php
session_start();

If (!$_SESSION['logged_in']) {

    header("location:login.php");
}

include "ConnectToDb.php";
$action = $_GET['method'];
$soruid = mysql_real_escape_string($_POST['question']);

If ($action == "image") {

    If ($_FILES['image'][name] != "") {

            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $extension = end(explode(".", $_FILES["image"]["name"]));
            $unique = md5(microtime());
            $date = date("Y-m-d H:i:s");
            $who = $_SESSION['username'];

            If ((($_FILES["image"]["type"] == "image/gif")
                || ($_FILES["image"]["type"] == "image/jpeg")
                || ($_FILES["image"]["type"] == "image/jpg")
                || ($_FILES["image"]["type"] == "image/pjpeg")
                || ($_FILES["image"]["type"] == "image/x-png")
                || ($_FILES["image"]["type"] == "image/png"))
                && ($_FILES["image"]["size"] < 500000000)
                && in_array($extension, $allowedExts)) {

                $pic = $unique. "." .$extension;
                move_uploaded_file($_FILES["image"]["tmp_name"], "answers/" . $pic);

                $ekle = "INSERT INTO `answers` (who, question_id, image, date) values('$who', '$soruid', '$pic', '$date')";
                $ok = mysql_query($ekle) or die (mysql_error());

                header("location:question.php?s=$soruid");

                }  

            }   


} elseif ($action == "text") {

    $text = mysql_real_escape_string($_POST['text']);
    $date = date("Y-m-d H:i:s");
    $who = $_SESSION['username'];

    $ekle = "INSERT INTO `answers` (who, question_id, comment, date) values('$who', '$soruid', '$text', '$date')";
    $ok = mysql_query($ekle) or die (mysql_error());

    header("location:question.php?s=$soruid");


}

?>

If the size of the image is low, such as 10kb or something like that, upload process works without a problem, but when i try to upload a file with 3 mb of size, script stops without a hitch.

I checked the php limits such as max_post_size, memory_limit etc. and seems like no problem.

I expect the script upload the image and then redirect the page.

What is the problem?

EDIT:

If I use the below code, it works.

$pic = "sercan.jpg";
move_uploaded_file($_FILES["image"]["tmp_name"], "answers/" . $pic);

So, I guess it means problem is with the code. But, Where is it?

EDIT 2:

Seems like the problem is with the if/else conditions. When I remove it, I am able to upload big-sized images.

If ((($_FILES["image"]["type"] == "image/gif")
            || ($_FILES["image"]["type"] == "image/jpeg")
            || ($_FILES["image"]["type"] == "image/jpg")
            || ($_FILES["image"]["type"] == "image/pjpeg")
            || ($_FILES["image"]["type"] == "image/x-png")
            || ($_FILES["image"]["type"] == "image/png"))
            && in_array($extension, $allowedExts)) {

..............
}
War es hilfreich?

Lösung 2

Changing this code:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["image"]["name"]));

To this:

$allowedExts = array("gif", "jpeg", "jpg", "png");          
$bits = explode(".", $_FILES["image"]["name"]);
$extension = strtolower(end($bits));

solved the problem.

Still not sure, How it is able to work with low-sized uploads while fails with high-sized uploads.

Whatever..

Andere Tipps

If the file is too large, then $_FILES is empty.

So, your if statement:

if ($_FILES['image']) ...

will not fire. Nor will your else, because that condition isn't being met. So the script completes without error, even though the image size is too large, and it did not upload.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top