PHP tot MySQL image uploading not working

前端 未结 2 734
小蘑菇
小蘑菇 2021-01-26 12:50

I\'m trying to make a site in which I can upload a file to my sql database, but it does not seem to work.

This is my code;


    
         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 13:23

    You should save the files in some folder during the upload process and save the name of file in database, so later you can call the name of file from database and link it as a hyperlink to download, i am using the following code to upload images in a folder called files and saving the name of files in database. At the end i have the file name in variable $newname

        if ($_FILES['file']['name']) {
    
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $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"] < 500000)
                && in_array($extension, $allowedExts)
            ) {
                if ($_FILES["file"]["error"] > 0) {
                    echo "Return Code: " . $_FILES["file"]["error"] . "
    "; } else { $ext = end(explode(".", $_FILES["file"]["name"])); $filename = current(explode(".", $_FILES["file"]["name"])); $newname = $filename . '_' . time() . '.' . $ext; move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $newname); } } else { echo "
    Image type or size is not valid.
    "; } }

提交回复
热议问题