php关于上传文件并保存到文件夹

时间:2015-03-15 16:27:31

标签: php mysql

尊敬” 这是我的前端脚本

    <form name="formRegister" method="post" action="confirmprod.php" onSubmit="return submitForms()">
    <table width="400" align="center" border="0">
    <tr>
        <td bgColor="c6d3ce">
          <table width="400" border="0">
            <tr bgColor="dee7e7">
              <td width="165">Name</td>
              <td><b><input type="text" id="Name" size="25" name="Name"></b></td>
            </tr>
            <tr bgColor="e7efef">
              <td>Path</td>
              <td><b><input type="file" size="25" name="Path"></b></td>
            </tr>
            <tr bgColor="e7efef">
              <td>Category</td>
              <td><b><input type="text" size="20" name="Category" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"></b></td>
            </tr>
            <tr bgColor="e7efef">
              <td>Price</td>
              <td><b><input type="text" maxlength="10" name="Price" size="20" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"></b></td>
            </tr>
            <tr bgColor="e7efef">
              <td>Description</td>
              <td><b><textarea cols="20" rows="2" name="Desc"></textarea></b></td>
            </tr>
            <tr bgColor="e7efef">
              <td>Type</td>
              <td><b><input type="text" size="20" name="Type"></b></td>
            </tr>
            <tr bgColor="dee7e7">
              <td>Views</td>
              <td><b><input type="text" maxlength="10" name="Views" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"></b></td>
            </tr>
            </table>
        </td>
    </tr>
    </table>
    <br>
    <table width="400" align="center" border="0">
      <tr>
        <td align="center" width="200"><input type="submit" value="Submit"></td>
        <td align="center" width="200"><input type="reset" name="reset" value="Reset Form" onClick="return confirm('Are you sure you want to reset the current information?');"></td>
      </tr>
    </table>
  </form>

这是我的后端代码

<?php
// Included configuration file in our code.
//include("includes/config.php");
include ("includes/mysqli_connection.php");

$prodname = mysqli_real_escape_string($db_conx,$_POST['Name']);
$path = mysqli_real_escape_string($db_conx,$_POST['Path']);
$category = mysqli_real_escape_string($db_conx,$_POST['Category']);
$price = mysqli_real_escape_string($db_conx,$_POST['Price']);
$desc = mysqli_real_escape_string($db_conx,$_POST['Desc']);
$type = mysqli_real_escape_string($db_conx,$_POST['Type']);
$views = mysqli_real_escape_string($db_conx,$_POST['Views']);

// This first query is just to get the total count of rows
$sql = "SELECT COUNT(*) FROM jewellery WHERE prodname = '$prodname'";

$query = mysqli_query($db_conx, $sql);

$row = mysqli_fetch_row($query);

// Here we have the total row count
$rows = $row[0];

if($rows == 0)
{   
    $insertSQL = "INSERT INTO jewellery (prodname, path, category, price, descr, type, noviews)
            VALUES ('$prodname', '$path', '$category', '$price', '$desc', '$type', '$views')";

    mysqli_query($db_conx, $insertSQL);

    if($insertSQL)
    {
        echo "<script>alert('Successfully Added!')</script>";

        echo "<script>window.location.href='viewprod.php'</script>";
    }
    else
    {
        echo 'An error occured while uploading the entry to database. Please try again later.';
    }
}

else
{
    echo "<font color='red'>Sorry This Product already exists!</font>";
    echo "<script>alert('Redirecting...')</script>";
    echo "<script>window.location.href='newprod.php'</script>";
}

// Close your database connection
mysqli_close($db_conx);
?>

主要问题是将文件上传到文件夹并在数据库中保存路径 在这个脚本中,我能够保存路径,但无法上传文件,任何人都可以帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

<?php
 // Included configuration file in our code.

include ("includes/mysqli_connection.php");

$prodname = mysqli_real_escape_string($db_conx,$_POST['Name']);
$category = mysqli_real_escape_string($db_conx,$_POST['Category']);
$price = mysqli_real_escape_string($db_conx,$_POST['Price']);
$desc = mysqli_real_escape_string($db_conx,$_POST['Desc']);
$type = mysqli_real_escape_string($db_conx,$_POST['Type']);
$views = mysqli_real_escape_string($db_conx,$_POST['Views']);
$view_target_path='';
 function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
if (!empty($_FILES["Path"]["name"])) {

    $file_name=$_FILES["Path"]["name"];
    $temp_name=$_FILES["Path"]["tmp_name"];
    $imgtype=$_FILES["Path"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".$price.time().$ext;
        $view_target_path="Photos/moses/".$imagename;
        $target_path="../Photos/moses/".$imagename;
 if(move_uploaded_file($temp_name, $target_path)) {
       // $name=$_REQUEST['name'];

//      mysql_query($query_upload) or die("error in $query_upload == ---->  ".mysql_error()); 
//            echo 'done';

    }else{

     exit("Error While uploading image on the server");
 }

    }
    $sql = "SELECT * FROM jewellery WHERE prodname = '$prodname'";

  $result = mysqli_query($db_conx, $sql);

if(!mysqli_num_rows($result))
{   
    $insertSQL = "INSERT INTO jewellery (prodname, path, category, price, descr, type, noviews)
            VALUES ('$prodname', '$view_target_path', '$category', '$price', '$desc', '$type', '$views')";

    mysqli_query($db_conx, $insertSQL);

    if($insertSQL)
    {
        echo "<script>alert('Successfully Added!')</script>";

        echo "<script>window.location.href='viewprod.php'</script>";
    }
    else
    {
        echo 'An error occured while uploading the entry to database. Please try again later.';
    }
}

else
{
    echo "<font color='red'>Sorry This Product already exists!</font>";
    echo "<script>alert('Redirecting...')</script>";
    echo "<script>window.location.href='newprod.php'</script>";
}

// Close your database connection
mysqli_close($db_conx);
?>