[英]PHP : add write permission to file after move_uploaded_file()
After an image is uploaded with PHP, I want to make the image file writable in order to add a watermark to it . 使用PHP上传图像后,我想使图像文件可写,以便为其添加水印。 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 . 上载文件后,文件权限变为644 。 Then I tried to use
chmod()
to change it to writable ( 666 ), but the permission doesn't change. 然后我尝试使用
chmod()
将其更改为可写( 666 ),但权限不会更改。
The /uploads
folder has permission 777 . /uploads
文件夹具有权限777 。 Is there any reason that makes chmod()
function fails to change permission? 是否有任何原因导致
chmod()
函数无法更改权限? or is there a workaround? 还是有解决方法?
Note: PHP 5 is used. 注意:使用PHP 5。 GD is working properly.
GD正常运作。
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
使用相对路径(例如
'uploads/' . $random_filename . $ext
时要非常小心'uploads/' . $random_filename . $ext
'uploads/' . $random_filename . $ext
'uploads/' . $random_filename . $ext
. '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. 其中magic
__DIR__
常量始终包含您实际所在文件的父目录的绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.