将图像上传到服务器并将图像路径保存到数据库

时间:2015-04-13 18:44:01

标签: php image-uploading

我有将代码上传到服务器的代码,但用于保存图像路径到数据库的PHP代码存在问题。它不会更新数据库。 它显示以下错误并且不能使用数据库:

 ( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 4
 ( ! ) Notice: Undefined index: datereg in C:\wamp\www\hosty\Uploads.php on line 8
 ( ! ) Notice: Undefined index: Description in C:\wamp\www\hosty\Uploads.php on line 9
 ( ! ) Notice: Undefined index: title in C:\wamp\www\hosty\Uploads.php on line 10
 ( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 26


<?php require_once('Connections/Conn_host.php'); ?>
<?php
 error_reporting(0);
  $target_dir = "uploads/";
   $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 $uploadOk = 1;
$datereg=$_POST['datereg'];
$Description=$_POST['Description'];
$title=$_POST['title'];
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
   }}
if (file_exists($target_file)) {

  echo "Sorry, file already exists.";
       $uploadOk = 0;
}
  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
     $uploadOk = 0;
  }
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
   echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
} 

 if ($uploadOk == 0) {
   echo "Sorry, your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],  $target_file)) {
       mysql_select_db($database_Conn_host, $Conn_host);
    $query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";
 $rsUpload = mysql_query($query_rsUpload, $Conn_host) or die(mysql_error());


       echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
}
}
   ?> 
 <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    <p>
      <label>Title
        <input name="title" type="text" id="title" />
      </label>
    </p>
<p>
    <label for="Description">Description</label>
     <textarea name="Description" id="Description" cols="45" rows="5">  </textarea>
  </p>
   <p>
     <input name="userId" type="hidden" id="hiddenField" />
     <input name="datereg" type="hidden" id="hiddenField2" />
  </p>
<p>Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
  </p>
</form>

 </body>
 </html>

2 个答案:

答案 0 :(得分:1)

<input type="file" name="image" size="50">  
<input type="Submit" name="submitted" value="Upload"
<?php
if(isset($_POST['submitted']))
{
 //connect to database
$file=$_FILES['image']['name'];
$file_tmp=$_FILES['image']['tmp_name'];

$path="your/path/".$file;
if(file_exists($path))
{
 chmod($path,0755);
  unlink($path);
}

if(move_uploaded_file($file_tmp,$path))
{
   echo "File uploaded succesfully";
   $sql = "UPDATE database ".
    "SET path = '$path'";
}  
}
?>     

希望这能解决您的问题。

答案 1 :(得分:0)

您的查询中存在错误。

$query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";

$Description之后,有两个逗号,,