0

I have the following code [x]:

    <form action="#" method="get" enctype="multipart/form-data">
    <label for="file">Filename:</label><br />
    <input type="text" name="type" placeholder="Type"><br />
    <input type="file" name="file"><br />
    <input type="submit" name="submit" value="Submit">
    </form>
<?
if($_GET['type']!="") {
    $type = $_GET['type'];
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    if($_FILES["file"]["type"] == "image/gif") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -4));
        $filename = $encryptfile. ".gif";
    } else if($_FILES["file"]["type"] == "image/jpeg") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -5));
        $filename = $encryptfile. ".jpeg";
    } else if($_FILES["file"]["type"] == "image/jpg") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -4));
        $filename = $encryptfile. ".jpg";
    } else if($_FILES["file"]["type"] == "image/pjpeg") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -6));
        $filename = $encryptfile. ".pjpeg";
    } else if($_FILES["file"]["type"] == "image/x-png") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -6));
        $filename = $encryptfile. ".x-png";
    } else if($_FILES["file"]["type"] == "image/png") {
        $encryptfile = md5(substr($_FILES["file"]["name"], 0, -4));
        $filename = $encryptfile. ".png";
    }
    $temp = explode(".", $filename);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2097152)
    && in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0) {
        echo '<a class="title" href="./upload.php">' .$_FILES["file"]["error"]. '</a>';
    } else {
        echo "Upload: " . $filename . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (!($stmt = $con->prepare("INSERT INTO `cat_images` (`title`,`path`) VALUES (?,?)")) || !is_object($stmt)) {
            die( "Error preparing: (" .$con->errno . ") " . $con->error);
        }
        $stmt->bind_param('ss', $type, $filename);
        if($stmt->execute()) {
            move_uploaded_file($_FILES["file"]["tmp_name"], "./assets/uploads" . $filename);
            echo "Stored in: " . "./assets/uploads/" . $filename . "<br />";
            echo '<a href="./">Home</a>';
        } else {
            echo 'SQL command could not be Executed, Contact a Staff Member';
            echo '<a href="./upload.php">Try Again</a>';
        }
        $stmt->close();
    }
    }
}
?>

I get no response from the page or server, so I don't know what the issue is. Do any of you know of the solution?

I am attempting to make an image upload form, but I don't get a response from the server for a website.

2
  • Method should be post ? Commented Apr 3, 2014 at 13:46
  • first thing first method="get" to method="POST" Commented Apr 3, 2014 at 13:46

2 Answers 2

1

From the php documentation of the superglobal $_FILES you can read the following sentence:

An associative array of items uploaded to the current script via the HTTP POST method.

So you should change your method="get" to method="post" and retrieve your $_GET['type'] by using $_POST['type'].

Sign up to request clarification or add additional context in comments.

Comments

0

i think problem is here...your code is...

 move_uploaded_file($_FILES["file"]["tmp_name"], "./assets/uploads" . $filename);

replace it with following line

 move_uploaded_file($_FILES["file"]["tmp_name"], "/assets/uploads/".$filename);

make sure all directory is exist that is /assets/uploads/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.