通过托管数据库中的php表单上传图像?

问题描述 投票:0回答:1

我们可以将图像从 php 表单上传到 Hostinger 托管数据库并将其存储在 Hostinger 文件夹中吗? 实际上我尝试这样做。我在本地主机上工作时得到了结果,但一旦我将其托管在托管服务器上,我就没有结果。

这是我的代码..

<?php
// Include the database configuration file
include 'dbConfig.php';
$statusMsg = '';

// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    $allowTypes = array('jpg','png','jpeg','gif','pdf');
    if(in_array($fileType, $allowTypes)){
       if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){          
            $insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");

            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            } 
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

// Display status message
echo $statusMsg;
?>

file-upload web-hosting file-handling image-upload
1个回答
0
投票

你是如何解决这个问题的?看来 Hostinger 太糟糕了

© www.soinside.com 2019 - 2024. All rights reserved.