无法在服务器PHP上保存文件

时间:2018-02-20 16:34:12

标签: php file-upload

$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $img["name"];
$filetype = $img["type"];
$filesize = $img["size"];

//checking file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed))
    die("Error: Please select a valid file format.");

$new_file_name = strtolower($img['tmp_name']); //rename file
$target_path = dirname(__FILE__) . "/images/";
move_uploaded_file($img["tmp_name"], $target_path . $new_file_name);

该文件应保存在我的/ images文件夹中,该文件夹位于根文件夹内,但我无法弄清楚它为什么不起作用,我很确定服务器接收文件,因为我可以从PHP获取其文件名及其临时位置(/ tmp / php +一些随机字符)

1 个答案:

答案 0 :(得分:0)

Try This:-

    $allowed = array("jpg" => "image/jpg",
                        "jpeg" => "image/jpeg",
                        "gif" => "image/gif", 
                        "png" => "image/png"
                        );
        $filename=$_FILES['file']['name'];
        $type=$_FILES['file']['type'];
        $tmp_name  = $_FILES['file']['tmp_name'];
        $size = $_FILES['file']['size'];

            //checking file extension
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            if(!array_key_exists($ext, $allowed))
                die("Error: Please select a valid file format.");

                $new_file_name = strtolower($tmp_name); //rename file
                $target_path = dirname(__FILE__) . "/images/";

            move_uploaded_file($tmp_name, $target_path.$filename);


            //in insert query use this $filename variable.