<?php
error_reporting(~E_NOTICE);
require_once 'dbconfig.php';
if($_POST)
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$description = $_POST['description'];
$election_name=$_POST['election_name'];
$category_name=$_POST['category_name'];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
echo $imgSize = $_FILES['user_image']['size'];
$upload_dir = 'user_images'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif'); // valid extensions
// rename uploading image
$photo = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($valid_extensions,$imgExt)) {
// Check file size '5MB'
if($imgSize > 5000000) {
move_uploaded_file($tmp_dir,"$upload_dir/$imgFile");
try {
$stmt = $db_con->prepare("INSERT INTO candidate(firstname,lastname,description,election_name,category_name,photo) VALUES(:ename, :edept, :esalary, :elect, :cat, :ima)");
$stmt->bindParam(":ename", $firstname);
$stmt->bindParam(":edept", $lastname);
$stmt->bindParam(":esalary", $description);
$stmt->bindParam(":elect", $election_name);
$stmt->bindParam(":cat", $category_name);
$stmt->bindParam(":ima", $photo);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
else{
echo "Sorry, your file is too large.";
}
}
else {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
?>
我有這樣的代碼上傳圖像和文本文件,但它說對不起,只有JPG,JPEG,PNG & GIF文件是允許的。我嘗試過多次解決,但是我無法自己找到解決方案,請提供正確的方法。上傳圖片到文件夾,並在PHP中插入數據庫的路徑和文本文件
.txt文件不在您允許的擴展名列表中... –
上傳哪個文件? –
發生了什麼?問題解決了? –