Php 图片上传不会进入设置目录
Php image upload wont go into set directory
我有完全相同的代码工作,尽管使用用户目录,将用户目录替换为媒体,并且它认为该文件夹不存在。我如何手动将图像放入媒体文件夹并通过我的域查看它。
这是我当前的代码。
<?php
session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])) {
// redirect to login page
}
$dbhost = "n.a";
$dbname = "n.a";
$dbuser = "n.a";
$dbpass = "n.a";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$ip = $_POST['ip'];
$date = $_POST['date'];
$time = $_POST['time'];
$UsrFName = $_POST['UsrFName'];
$UsrLName = $_POST['UsrLName'];
$UsrID = $_POST['UsrID'];
$post_id = $_POST['post_id'];
$format = $_POST['format'];
$file_format = $_POST['file_format'];
$MediaTxt = $_POST['MediaTxt'];
$author_id = $_POST['author_id'];
$target_dir = ('media/');
$target_file = $target_dir . $post_id .'.'.pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
// Check file size
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "gif" && $imageFileType != "GIF" && $imageFileType != "tiff" && $imageFileType != "TIFF" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "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 header('Location: main.php');
}
}
$errmsg_arr = array();
$errflag = false;
if($ip == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($date == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($time == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrFName == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrLName == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrID == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($post_id == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($format == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($file_format == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($MediaTxt == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = false;
}
if($author_id == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: test-error.php");
exit();
}
// query
$sql = "INSERT INTO n.a (ip,date,time,firstname,lastname,userID,post_id,format,file_format,MediaTxt,author_id) VALUES
(:sasa,:sasad,:sasadf,:sasafds,:sasafda,:sasdfhj,:sasdhadka,:sasdhadkas,:sasdhadkasdas,:sashasdas,:aauutthorrid)";
$q = $conn->prepare($sql);
$q->execute(array(':sasa'=>$ip,':sasad'=>$date,':sasadf'=>$time,':sasafds'=>$UsrFName,':sasafda'=>$UsrLName,':sasdfhj'=>$UsrID,
':sasdhadka'=>$post_id,':sasdhadkas'=>$format,':sasdhadkasdas'=>$file_format,':sashasdas'=>$MediaTxt,':aauutthorrid'=>$author_id));
header("location: main.php");
?>
您是否检查了要上传到的新文件夹的权限?
我有完全相同的代码工作,尽管使用用户目录,将用户目录替换为媒体,并且它认为该文件夹不存在。我如何手动将图像放入媒体文件夹并通过我的域查看它。
这是我当前的代码。
<?php
session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])) {
// redirect to login page
}
$dbhost = "n.a";
$dbname = "n.a";
$dbuser = "n.a";
$dbpass = "n.a";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$ip = $_POST['ip'];
$date = $_POST['date'];
$time = $_POST['time'];
$UsrFName = $_POST['UsrFName'];
$UsrLName = $_POST['UsrLName'];
$UsrID = $_POST['UsrID'];
$post_id = $_POST['post_id'];
$format = $_POST['format'];
$file_format = $_POST['file_format'];
$MediaTxt = $_POST['MediaTxt'];
$author_id = $_POST['author_id'];
$target_dir = ('media/');
$target_file = $target_dir . $post_id .'.'.pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
// Check file size
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "gif" && $imageFileType != "GIF" && $imageFileType != "tiff" && $imageFileType != "TIFF" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "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 header('Location: main.php');
}
}
$errmsg_arr = array();
$errflag = false;
if($ip == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($date == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($time == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrFName == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrLName == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($UsrID == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($post_id == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($format == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($file_format == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($MediaTxt == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = false;
}
if($author_id == '') {
$errmsg_arr[] = 'You must enter your Email';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: test-error.php");
exit();
}
// query
$sql = "INSERT INTO n.a (ip,date,time,firstname,lastname,userID,post_id,format,file_format,MediaTxt,author_id) VALUES
(:sasa,:sasad,:sasadf,:sasafds,:sasafda,:sasdfhj,:sasdhadka,:sasdhadkas,:sasdhadkasdas,:sashasdas,:aauutthorrid)";
$q = $conn->prepare($sql);
$q->execute(array(':sasa'=>$ip,':sasad'=>$date,':sasadf'=>$time,':sasafds'=>$UsrFName,':sasafda'=>$UsrLName,':sasdfhj'=>$UsrID,
':sasdhadka'=>$post_id,':sasdhadkas'=>$format,':sasdhadkasdas'=>$file_format,':sashasdas'=>$MediaTxt,':aauutthorrid'=>$author_id));
header("location: main.php");
?>
您是否检查了要上传到的新文件夹的权限?