doutan1875 2010-06-29 14:37
浏览 273
已采纳

上传问题(权限被拒绝)

I'm currently using the file component in the vork framework to upload a file and I keep getting this error:

Warning: move_uploaded_file(/uploads) [function.move-uploaded-file]: failed to open stream: Permission denied in /var/www/rto-vork/mvc/components/file on line 105

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php3WC6QP' to '/uploads' in /var/www/rto-vork/mvc/components/file on line 105 string(32) "Could not move the uploaded file" success

I believe the component itself is fine, and the uploads directory has already been chmoded to 777

here's the code for the upload the file id is being passed in correctly

public function uploadFile($id, $destination, $imagesOnly = false) {

    $return = false;

    if (substr($_FILES[$id]['name'], 0, 1) == '.') {

        $return = 'File names must not begin with a dot';

    } else {

        $isInvalidUpload = $this->isInvalidUpload($id, $imagesOnly);

        if ($isInvalidUpload) {

            $return = $isInvalidUpload;

        } else {

            if (move_uploaded_file($_FILES[$id]['tmp_name'], $destination)) {

                if (is_dir($destination)) {

                    if ($destination[-1] != '/' && $destination[-1] != '\\') {

                        $destination .= '/';

                    }

                    $destination .= $_FILES[$id]['tmp_name'];

                }

                chmod($destination, 0777);

            } else {

                $return = 'Could not move the uploaded file';

            }

        }

    }

    return $return;

}
  • 写回答

3条回答 默认 最新

  • dongyong9224 2010-06-29 17:06
    关注

    Your code's flawed:

    public function uploadFile($id, $destination, $imagesOnly = false) {
       ...
       if (move_uploaded_file(...)) {
           if (is_dir(...)) {
               ...
           }
        } else {
           $return = 'Could not move the uploaded file';
        }
    }
    

    You call the method with uploadFile('somename', '/uploads'), so the code first tries to move the uploaded file to /uploads directly, but this is a directory. move_uploaded_file() does not act like a regular filesystem 'move' command, the source AND target both have to be filenames. So your initial move attempt fails, it returns false, and things jump directly to the "could not move file" return call. It never even tries to handle $destination being a directory

    You should restructure the method as follows:

    public function uploadFile(...) {
        ...
        // if $destination is a directory, handle that fact
        if (is_dir($destination)) {
           $destination .= '/' . $new_file_name;
        }
        // THEN try to move the file
        if (move_uploaded_file($source, $destination)) {
           return('worked');
        } else {
           return('failed');
        }
    }
    

    Also, be aware that while it's unlikely, the temporary filename that PHP assigns is NOT guaranteed to be unique over time. It's entirely possible that at some point down the line, the same random name will be generated again, and you'll overwrite an older upload. You should be using a filename that is guaranteed to be unique, like a primary key 'id' field from a database or some other source of non-repeating data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘