无法使用原始名称移动上传的文件

时间:2015-10-15 12:21:45

标签: php file file-upload

我正在尝试将我的图像名称存储在数据库中,并将图像存储在文件夹中。图像正在移动但问题是图像没有以原始名称存储。一些临时名称(图像)前缀为我的图像名称。

我的代码:

//This is the directory where images will be saved
$target = 'image/image';
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$pic=($_FILES['photo']['name']);
$query1 = mysql_query("SELECT * from image WHERE image_name = '$pic' ");

if(!$query1) {
    echo "error";
}

$rows = mysql_num_rows($query1);
if ($rows != 1) {
    //Writes the information to the database
    $query = mysql_query("INSERT INTO image(image_name) VALUES ('$pic')");

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
        //Tells you if its all ok
        echo "The file  has been uploaded, and your information has been added to the directory";
    }
    else {
        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
    }
}
else {
    echo "image already exits in database";
}

如何解决这个问题。任何线索?

4 个答案:

答案 0 :(得分:1)

您使用的代码:

//This is the directory where images will be saved
$target = 'image/image';
$target = $target . basename( $_FILES['photo']['name']);

如果您使用'image/image',则图片将存储在image/目录下,其前缀为image和image_name,即imageimage_name.jpg

如果您在使用image/image/时遇到任何错误,请检查您的目录结构是'image/image/'还是仅image/

答案 1 :(得分:0)

试试这段代码

$target= 'image/image'; 
$imagename = $_FILES['photo']['name'];
$target = $target.$imagename;
$temp = $_FILES['photo']['tmp_name']
echo '<pre>';
if (move_uploaded_file($temp, $target)) {
    echo "File is  successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

答案 2 :(得分:0)

<?php

    /* This is the directory where images will be saved */
    $target = 'image/image';
    /* Full target path for uploaded image */
    $target = $target . DIRECTORY_SEPARATOR . basename( $_FILES['photo']['name'] );



    /* This gets all the other information from the form */
    $pic=$_FILES['photo']['name'];
    $query1 = mysql_query( "SELECT * from `image` WHERE `image_name` = '$pic';" );



    if( !$query1 ) {

        echo "error querying database";

    } else {

        $rows = mysql_num_rows( $query1 );

        /* If there are no records in db for this piture, add a record */
        if ( $rows == 0 ) {

            /* Writes the information to the database */
            $query = mysql_query("INSERT INTO `image` (`image_name`) VALUES ('$pic');");

            /* Writes the photo to the server */
            if( move_uploaded_file( $_FILES['photo']['tmp_name'], $target ) ) {

                /* Tells you if its all ok */
                echo "The file  has been uploaded, and your information has been added to the directory";

            } else {

                /* Gives and error if its not */
                echo "Sorry, there was a problem uploading your file.";

            }
        } else {
            echo "image already exits in database";
        }
    }

?>

答案 3 :(得分:0)

$file = $_FILES['fileup']['name'];
$tmp_name = $_FILES['fileup']['tmp_name'];
$target_dir = "uploads/".basename($file);
if($file!="") {
    if(move_uploaded_file($tmp_name,$target_dir)) {
        echo 'success';
    } else {
        echo 'not success';
    }
}