duanmei1694 2015-05-09 15:06
浏览 35
已采纳

Php图片上传不会进入设置目录

I had the exact same code working, although with users directory, replaced the users dir with media, and its like it doesn't think the folder exists. How ever I can manualy put a image inside the media folder and view it via my domain.

Here is my current code.

<?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");


?>
  • 写回答

1条回答 默认 最新

  • dongshanjin8947 2015-05-09 15:10
    关注

    Have you checked the permissions of the new folder you're trying to upload to?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题