php move_uploaded_file()

时间:2012-04-24 04:14:48

标签: php file-upload upload

所以我正在测试w3schools网站http://www.w3schools.com/php/php_file_upload.asp中的move_uploaded_file()php脚本。 这是我的代码。

if ($_FILES["file"]["size"] < 2000000)
{
    if ($_FILES["file"]["error"] > 0)
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("/var/www/upload/" . $_FILES["file"]["name"]))
        {
          echo $_FILES["file"]["name"] . " already exists. ";
        }
        elseif(move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/upload/".$fileName))
            echo "Stored in: " . "/var/www/upload/".$fileName;
    }
}
else
    echo "Invalid file";

问题是if(move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/upload/".$fileName))一直返回false,但似乎文件存储在服务器上的tmp文件夹中(例如:/tmp/php8rrKoW)。当我检查tmp文件夹时,文件不存在。 (它应该在脚本完成执行后被删除。)我也没有看到/php8rrkoW文件夹。我不确定它是否应该在那里。我使用tmp/var/www/upload/文件夹和777的权限设置为chmod,但我不确定是否应将所有者设置为apache 。所以我想知道为什么文件没有复制到/var/www/upload,如果有办法测试它。

4 个答案:

答案 0 :(得分:1)

这是我前几天为另一个问题制作的基本图片上传课程,简单易用,也许你发现它很有趣。

<?php 
error_reporting(E_ALL); //Will help you debug a [server/path/permission] issue
Class uploadHandler{
    public $upload_path;
    public $full_path;
    public $name;
    public $size;
    public $ext;
    public $output;
    public $input;
    public $prefix;
    private $allowed;

    function upload(){
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            if(isset($_FILES[$this->input]['error'])){
                if($_FILES[$this->input]['error'] == 0){
                    $this->name      = basename($_FILES[$this->input]['name']);
                    $file_p          = explode('.', $this->name);
                    $this->ext       = end($file_p);
                    $this->full_path = rtrim($this->upload_path,'/').'/'.preg_replace('/[^a-zA-Z0-9.-]/s', '_', $this->prefix.'_'.$file_p[0]).'.'.$this->ext;
                    $info            = getimagesize($_FILES[$this->input]['tmp_name']);
                    $this->size      = filesize($_FILES[$this->input]['tmp_name']);

                    if($info[0]>$this->allowed['dimensions']['width'] || $info[1] > $this->allowed['dimensions']['height']){
                        $this->output = 'File dimensions too large!';
                    }else{
                        if($info[0] > 0 && $info[1] > 0 && in_array($info['mime'],$this->allowed['types'])){
                            move_uploaded_file($_FILES[$this->input]['tmp_name'],$this->full_path);
                            $this->output = 'Upload success!';
                        }else{
                            $this->output = 'File not supported!';
                        }
                    }
                }else{
                    if($_FILES[$this->input]['error']==1){$this->output = 'The uploaded file exceeds the upload_max_filesize directive!';}
                    if($_FILES[$this->input]['error']==2){$this->output = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in our HTML form!';}
                    if($_FILES[$this->input]['error']==3){$this->output = 'The uploaded file was only partially uploaded!';}
                    if($_FILES[$this->input]['error']==4){$this->output = 'No file was uploaded!';}
                    if($_FILES[$this->input]['error']==6){$this->output = 'Missing a temporary folder!';}
                    if($_FILES[$this->input]['error']==7){$this->output = 'Failed to write uploaded file to disk!';}
                    if($_FILES[$this->input]['error']==8){$this->output = 'A PHP extension stopped the file upload!';}
                }
            }
        }
    }

    function setPath($var){
        $this->upload_path = $var;
    }
    function setAllowed($var=array()){
        $this->allowed = $var;
    }
    function setFilePrefix($var){
        $this->prefix = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $var);
    }
    function setInput($var){
        $this->input = $var;
    }

}



//Start class
$upload = new uploadHandler();
//Set path
$upload->setPath('./');
//Prefix the file name
$upload->setFilePrefix('user_uploads');
//Allowed types
$upload->setAllowed(array('dimensions'=>array('width'=>200,'height'=>200),
                          'types'=>array('image/png','image/jpg','image/gif')));
//form property name                   
$upload->setInput('myfile');
//Do upload
$upload->upload();


//notice
if(isset($upload->output)){
    echo $upload->output;
}
?>

<form action="" method="POST" enctype="multipart/form-data">
     <!--1 MB = 1048576 bytes-->
     <input type="hidden" name="MAX_FILE_SIZE" value="1048000" />

     <p>Upload your image:<input type="file" name="myfile"><input type="submit" value="Upload"></p>

</form>

答案 1 :(得分:0)

您似乎应该使用./upload/而不是/ var / www / upload /,因为您已经在可访问网站的主目录中。

您还可以参考http://www.tizag.com/phpT/fileupload.php 或API:http://php.net/manual/en/function.move-uploaded-file.php

如果有效,请告诉我

答案 2 :(得分:0)

要将文件移动到的目标目录应该可由webserver用户写入。另外请不要忘记,某些Web服务器在更改根目录内运行,因此可能不需要部分目标路径。 PHP帮助文档还说你应该检查HTLM表单以确保它的enctype ='multipart / form-data'。

最后:$ filename定义在哪里?

答案 3 :(得分:0)

您的路径必须包含&#39; / home / usr-name&#39;

尝试添加&#39; / home / your-username&#39;到&#39; / var / www / upload&#39;的开头

要想到这个想法,请将info.php添加到根目录中,在浏览器中打开并查看“已加载的配置文件”。