将图像上载到文件夹,并在php中插入数据库和文本文件的路径
我有这个代码上传图像和文本文件,但它说对不起,只有JPG,JPEG,PNG和GIF文件是允许的。我已多次尝试解决此问题,但我自己找不到解决方案,请建议正确的方法。只需在ur aarray中添加将图像上载到文件夹,并在php中插入数据库和文本文件的路径,php,image-uploading,Php,Image Uploading,我有这个代码上传图像和文本文件,但它说对不起,只有JPG,JPEG,PNG和GIF文件是允许的。我已多次尝试解决此问题,但我自己找不到解决方案,请建议正确的方法。只需在ur aarray中添加txt扩展名即可 <?php error_reporting( ~E_NOTICE ); require_once 'dbconfig.php'; if($_POST) { $firstname = $_POST['firstname']; $lastname = $_POST['lastn
txt
扩展名即可
<?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.";
}
?>
你需要改变
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif', 'txt');
到
需要第一个参数作为搜索值,第二个参数作为数组
您总是返回false值,并且代码中出现错误,如下所示
if (in_array($imgExt,$valid_extensions)) {
希望能有所帮助。您只需更正这一行中的代码即可:
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','.txt');
它会起作用的
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions
您需要使用此代码修改代码…此代码将在文件夹中插入图像,并在数据库中插入路径:
if(in_array($imgExt,$valid_extensions))
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt');
根据我的说法
1)您编写的文本文件也将上载。但是,没有将文本文件扩展名添加到$valid\u extensions
改为
<?php
$grocery=$_POST['grocery'];
$connect=new mysqli("localhost","root","");
mysqli_select_db($connect,'go-web');
$target_dir = "..images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
die("File is not an image.");
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
die("Sorry, file already exists.");
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
die("Sorry, your file is too large.");
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
die("Sorry, only JPG, JPEG & PNG files are allowed.");
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
die("Sorry, your file was not uploaded.");
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
die("Sorry, there was an error uploading your file.");
}
}
$query=mysqli_query($connect,"INSERT INTO product VALUES ('$grocery','$target_file')");
if($query)
echo "Uploaded";
?>*
2)正如@Saty所回答的,您需要在
if(在数组中($valid\u extensions,$imgExt)){
作为
if(在数组中($imgExt,$valid\u扩展名)){
第一个参数充当搜索值
,第二个参数充当搜索数组
。txt文件不在您允许的扩展数组中…您上载的是哪个文件?发生了什么?问题解决了吗?问题仍然存在谢谢大家我只是那样更改了,但仍然说对不起,只允许JPG、JPEG、PNG和GIF文件。什么是您必须上载的文件名??上一个是上载图像的输入表单我的计算机中的文件类型是.jpgecho$imgExt=strtolower(pathinfow($imgFile,pathinfo_扩展名));
//获取图像扩展名并粘贴其值
<?php
$grocery=$_POST['grocery'];
$connect=new mysqli("localhost","root","");
mysqli_select_db($connect,'go-web');
$target_dir = "..images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
die("File is not an image.");
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
die("Sorry, file already exists.");
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
die("Sorry, your file is too large.");
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
die("Sorry, only JPG, JPEG & PNG files are allowed.");
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
die("Sorry, your file was not uploaded.");
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
die("Sorry, there was an error uploading your file.");
}
}
$query=mysqli_query($connect,"INSERT INTO product VALUES ('$grocery','$target_file')");
if($query)
echo "Uploaded";
?>*
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions