php - 为什么move_uploaded_file不允许我将它存储到我的驱动器?

时间:2014-02-20 07:28:55

标签: php file-upload

下面的代码演示假设将我的图像存储到一个目录中,但是,使用xammp我很困惑如果我需要权限将它存储在那里因为我收到警告并且文件没有保存在我想要的地方去吧。

<?php
//simple image check using getimagesize() instead of extensions
if($_FILES)
{
    $empty_check = getimagesize($_FILES['file']['tmp_name']);
    if(empty($empty_check))
    {
        echo 'this is not an image';
    }
    else
    {
        echo 'you have uploaded ' . explode('.',$_FILES['file']['name'])[0].' and it is a ' . explode('.',$_FILES['file']['name'])[1].'.';
        //an example of how i would extract the extension
        $target = "C:\xampp\tmp";
        move_uploaded_file($_FILES['file']['tmp_name'], $target);
    }
}
?>

3 个答案:

答案 0 :(得分:1)

逃离路径中的反斜杠!

$target = "C:\xampp\tmp";
//should be
$target = "C:\\xampp\\tmp";

考虑\t被解释为标签,您的路径如下所示:

$target = "C:[?]ampp    mp";

但实际上,你可以使用正斜杠:

$target = '/xampp/tmp';

如果仍然无效,请确保您对该目录具有写权限,并在必要时进行修复

然后,最后read the docs

move_uploaded_file($_FILES['file']['tmp_name'], $target.'/filename_here.ext');

将文件名添加到目标参数!

答案 1 :(得分:0)

尝试这样的事情

<?php 

//simple image check using getimagesize() instead of extensions

if($_FILES){
    $empty_check = getimagesize($_FILES['file']['tmp_name']);
    if(empty($empty_check)){
        echo 'this is not an image';
    }else{
        $exploded = explode('.',$_FILES['file']['name']);
        echo 'you have uploaded ' . current($exploded).' and it is a '.end($exploded);
        $target = 'C:\\xampp\\tmp\\'. $_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], $target);

    }

}
?>

答案 2 :(得分:0)

我猜错了另一个斜线,

此代码应该可以使用,也可以使用DIRECTORY_SEPARATOR代替\

$target = 'C:\xampp\tmp\';
move_uploaded_file($_FILES['file']['tmp_name'], $target);

正确的方法:

$target = $someDir . DIRECTORY_SEPARATOR.'xampp'.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR;