PHP图片上传问题?

时间:2014-04-03 13:44:28

标签: php

我有以下代码 [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();
    }
    }
}
?>

我没有收到来自页面或服务器的回复,所以我不知道问题是什么。 你们中的任何人都知道解决方案吗?

我正在尝试制作图片上传表单,但我没有收到服务器对网站的回复。

2 个答案:

答案 0 :(得分:1)

从超级全球$_FILES的php文档中,您可以阅读以下句子:

  

通过HTTP POST方法上传到当前脚本的关联项目数组。

因此,您应该将method="get"更改为method="post"并使用$_GET['type']检索$_POST['type']

答案 1 :(得分:0)

我认为问题在这里......你的代码是......

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

将其替换为以下行

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

确保所有目录都存在/ assets / uploads /