简体   繁体   English

将文件移到目录PHP

[英]Move file to directory PHP

Tried many things but i can't get it working. 尝试了很多事情,但我无法正常工作。

I have a sub directory with the name "pics" and i need to upload images there with my upload.php file. 我有一个名为“ pics”的子目录,我需要使用我的upload.php文件将图像上传到该目录。

How can i do this? 我怎样才能做到这一点? Here is some of my code where i think i need to add the directory. 这是我的一些代码,我认为我需要添加目录。

if(is_uploaded_file($_FILES['inputfile']['tmp_name'])){


    if(!file_exists($_FILES['inputfile']['name'])) {
        move_uploaded_file($_FILES['inputfile']['tmp_name'], $_FILES['inputfile']['name']);
    }

Thank you very much for your help. 非常感谢您的帮助。

Try this , you should place the folder at the correct root , i will assume that the php code is at the same folder. 试试这个,您应该将文件夹放置在正确的根目录下,我将假设php代码在同一文件夹中。 If not you need to direct the code to the right path. 如果不是,则需要将代码定向到正确的路径。 Finding your current path can be achived by using 使用以下命令可以找到您的当前路径

echo __FILE__ ;

for example with your code i see that you dont place the path correct 例如,用您的代码,我看到您没有正确放置路径

So you mat try this.. 所以你可以试试这个。

$dir_name = '/pics';
if( move_uploaded_file($_FILES["files"]["tmp_name"], $dir_name) )

You have to add the path when you do the check file exists and add path to destination file as well. 当您检查文件是否存在时,必须添加路径,并将路径也添加到目标文件。

if (is_uploaded_file($_FILES['inputfile']['tmp_name'])) {

    $path = 'path/to/pics/';

    if (!file_exists($path . $_FILES['inputfile']['name'])) {

        move_uploaded_file($_FILES['inputfile']['tmp_name'], $path . $_FILES['inputfile']['name']);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM