上传过程不会在数据库中保存正确的名称

时间:2015-05-08 16:01:28

标签: php html mysql forms

我在php中有一个代码验证确定所有工作正常但我的问题是,当我尝试保存在数据库中时,我得到这样的东西:

img_id   img_small           img_big           
  5      /tmp/phpdlYkiG      /tmp/phph3dhka

我不知道为什么php会保存这个名字,因为这些图片有不同的名字,比如koala.jpg和horse.jpg

这是我的代码,以便查看某人是否有任何建议......

<form enctype="multipart/form-data" action="upload_type_1.php" method="POST" >        
  <input type="file" name="img_small_1" id="img_small_1">
  <input type="file" name="img_big_1" id="img_big_1">

  <input type="submit" value="Upload" name="submit">                                   
</form>

这是我的PHP代码:

if ( (move_uploaded_file($_FILES["img_small_1"]["tmp_name"], $target)) && (move_uploaded_file($_FILES["img_big_1"]["tmp_name"], $target2)) ){
    $img_title_1 = $_POST['img_title_1'];

    $sql = "INSERT INTO press (img_title, img_small, img_big) VALUES ('$img_title_1', '$img_small_1', '$img_big_1')";

    $retval = mysql_query( $sql, $conn );

    if(!$retval) {
      die('Could not enter data: ' . mysql_error());
    }

    mysql_close($conn);

    echo "Your files has been uploaded";        

} else {

    echo "Sorry, there was an error uploading your files."; 

    exit;
}

这段代码正常工作唯一的问题是将奇怪的名字保存到数据库中我需要使用这些名称......

谢谢! - 等待帮助!

3 个答案:

答案 0 :(得分:2)

您的问题可能不在您展示的代码中,而是在您未展示的代码中,这是您$img_small_1&amp;&amp;的变量声明。 $img_big_1。猜猜你有

$img_small_1 = $_FILES["img_small_1"]["tmp_name"];
$img_big_1 = $_FILES["img_big_1"]["tmp_name"];

但你想/需要

$img_small_1 = $_FILES["img_small_1"]["name"];
$img_big_1 = $_FILES["img_big_1"]["name"];

答案 1 :(得分:0)

$img_title_1 = $_POST['img_title_1'];

应该是:

$img_title_1 = $_FILES["img_small_1"]["name"]

答案 2 :(得分:0)

文件上传的简单示例

$uploadDir = "Your_upload_dir";
$img_small = $_FILES['img_small_1'];

$img_small_name = $img_small['name']; // get image name
$img_small_tmpName = $img_small['tmp_name'];
$img_small_fileSize = $img_small['size'];
$img_small_fileType = $img_small['type'];

if ($img_small['error'] == 0)
{
    $img_small_filePath = $uploadDir . $img_small_name;
    $result = move_uploaded_file($img_small_tmpName,  img_small_filePath); //return true or false

}