PHP Move_uploaded_file没有将我的文件移动到目录

时间:2017-03-31 20:59:20

标签: php upload

我正在为我正在开发的程序制作图像上传功能但是我似乎无法通过move_uploaded_file来实际移动我的文件而且我不确定原因。

我收到表单数据,因为当我使用print_r函数时可以看到数据print_r($_FILES)

我的目录结构如下 root->app->lib->uploadCode.phproot->app->images

我没有收到任何错误。 我在安装了php7的ubuntu 16.04上运行它

这是我的代码。

<?php
error_reporting(E_ALL);
if(!empty($_FILES['newImage']['name']))
{
    $fname = $_FILES['newImage']['name'];
    $fext = explode(".",$fname);
    $sex = array("jpg", "jpeg", "JPG", "JPEG", "png", "PNG");//Supported extensions
    //Check if file extension supported
    $extRes = array_intersect($fext, $sex);//Checks to see if file uploaded extension exists in the supported extension array
    if(count($extRes) == 1)
    {
        print_r($_FILES);
        $name = $_FILES['newImage']['name'];
        $ext = end($fext);
        $tmp = $_FILES['newImage']['tmp_name'];
        $path = '/../images/';
        $newName = uniqid().".".$ext;
        /*if(move_uploaded_file($tmp,$path.$newName))
        {
            echo "uploaded";
        }
        else
            {
                echo "not uploaded";
                echo $path.$newName;
            }*/

        move_uploaded_file($tmp,$path.$newName);
    }
    elseif(count($extRes) > 1)
        {//More than just one file extension was found possibly something like image.png.jpg
            echo 02;
        }
        else
            {// Nothing was found so wrong file type was used
                echo 01;
            }
}else
    {//Do if no image was uploaded
        echo 00;
    }

`

2 个答案:

答案 0 :(得分:0)

$path = '/../images/';移除左侧斜线或设置完整路径,例如例如/home/username/images/

答案 1 :(得分:0)

这是一个许可问题。

我必须在我的www / html目录中执行以下操作。

<div class="col-lg-4 col-md-6 col-sm-12"></div>  sudo chown -R username:www-data /var/www/html/  sudo chmod 775 /var/www/html/ 一旦我运行了这些命令,我​​就可以上传