如何上传特定文件夹中的图像php?

时间:2016-02-12 07:04:28

标签: php file directory

我安装了xampp,我有一个程序可以选择一个文件并将其上传到我的服务器。我在htdocs中有一个名为uploads的文件夹,用于存储图片。当我上传它们时,它会进入htdocs,但不是我想要的htdocs中的文件夹。我确实指明我需要它去那里。有人可以帮忙吗?

下面是代码:

<?php
@$name = $_FILES['file']['name'];
@$size = $_FILES['file']['size'];
@$type = $_FILES['file']['type'];
@$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
    if (!empty($name)) 
    {
    $location = 'uploads/';
    if (move_uploaded_file($tmp_name, $location. $name));
    echo 'Uploaded';
    }
    else 
    {
        echo 'Please choose a file';
    }

}
?>


<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>

1 个答案:

答案 0 :(得分:3)

尝试以下代码:
 注意:确保您的uploads文件夹具有写入权限。

<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");

$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];

if (isset($name)) 
{
    if (!empty($name)) 
    {
        if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
            echo 'Uploaded';
        else
            echo "Not Uploaded";
    }
    else 
    {
        echo 'Please choose a file';
    }

}
?>