PHP:在move_uploaded_file()之后为文件添加写入权限 [英] PHP : add write permission to file after move_uploaded_file()

查看:545
本文介绍了PHP:在move_uploaded_file()之后为文件添加写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP上传图像后,我想使图像文件可写,以便为其添加水印。这里是我使用的代码:

pre $ if(isset($ _ FILES ['file_poster'] ['tmp_name'])& & $ _FILES ['file_poster'] ['tmp_name']!=''){
$ random_filename = substr(md5(time()),0,9);
$ ext ='.jpg';
if(strpos(strtolower($ _ FILES ['file_poster'] ['name']),'.png')> -1){
$ ext ='.png';

$ b move_uploaded_file($ _ FILES ['file_poster'] ['tmp_name'],'uploads /'。$ random_filename。$ ext);
chmod(ABS_PATH。$ random_filename,0666);
$ random_filename ='uploads /'。 $ random_filename。 $ EXT;

//添加水印代码省略
}

文件上传,文件权限变为 644 。然后,我尝试使用 chmod()将其更改为可写( 666 ),但权限不会更改。 b
$ b

/ uploads 文件夹具有 777 权限。是否有任何理由让 chmod()函数无法更改权限?或者是否有解决方法?



注意:使用PHP 5。 GD正在正常工作。

解决方案

看起来你需要交换最后两行,例如

  $ random_filename ='uploads /'。 $ random_filename。 $ EXT; 
chmod(ABS_PATH。$ random_filename,0666);

使用'uploads /'等相对路径时要非常小心。 $ random_filename。 $分机。如果工作文件被包含到另一个文件中,那么当前目录可能会有所不同。



我会推荐这样的内容

  $ destFile = __DIR__。 '/ uploads /'。 $ random_filename。 $ EXT; 
move_uploaded_file($ _ FILES ['file_poster'] ['tmp_name'],$ destFile);
chmod($ destFile,0666);

其中 __ DIR __ 常量包含绝对路径到您实际所在文件的父目录。


After an image is uploaded with PHP, I want to make the image file writable in order to add a watermark to it . Here are the codes I used:

if(isset($_FILES['file_poster']['tmp_name']) && $_FILES['file_poster']['tmp_name'] != '') {
        $random_filename = substr(md5(time()), 0, 9);
        $ext = '.jpg';
        if(strpos(strtolower($_FILES['file_poster']['name']), '.png') > -1) {
            $ext = '.png';
        }

        move_uploaded_file($_FILES['file_poster']['tmp_name'], 'uploads/' .  $random_filename . $ext);
        chmod(ABS_PATH . $random_filename, 0666);
        $random_filename = 'uploads/' .  $random_filename . $ext;

        // add watermark codes omitted
}

After the file is uploaded, the file permission becomes 644. Then I tried to use chmod() to change it to writable ( 666 ), but the permission doesn't change.

The /uploads folder has permission 777. Is there any reason that makes chmod() function fails to change permission? or is there a workaround?

Note: PHP 5 is used. GD is working properly.

解决方案

Looks like you need to swap your last two lines, eg

$random_filename = 'uploads/' .  $random_filename . $ext;
chmod(ABS_PATH . $random_filename, 0666);

Be very careful when using relative paths such as 'uploads/' . $random_filename . $ext. If the working file is included into another file, the current directory may differ.

I would recommend something like this

$destFile = __DIR__ . '/uploads/' . $random_filename . $ext;
move_uploaded_file($_FILES['file_poster']['tmp_name'], $destFile);
chmod($destFile, 0666);

where the magic __DIR__ constant always contains the absolute path to the parent directory of the file you're actually in.

这篇关于PHP:在move_uploaded_file()之后为文件添加写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆