Skip to main content

ไม่สามารถสร้างไฟล์ หรือโฟลเดอร์ที่มี Permission 777 ได้ใน Linux

หลายคนอาจเคยประสบปัญหาเกี่ยวกับการสร้างไฟล์ หรือโฟลเดอร์บน Linux ด้วยคำสั่ง PHP เช่น mkdir ซึ่งถึงแม้จะกำหนด Permission ในขั้นตอนการสร้างให้เป็น 0777 แล้ว แต่พอไปดูที่ Folder จริงจะไม่เป็นไปตามที่กำหนดอาจจะเห็นเป็น rwxr-xr-x หรือ 0755 นั่นเอง ปัญหาที่เกิดขึ้นนี้มักสร้างปัญหาในกรณีที่มีการสร้าง Folder ที่ใช้ร่วมกันในหลาย Server ซึ่งอาจทำให้ Server อื่นไม่สามารถเขียนไฟล์ทับ หรือเขียนลงใน Folder ได้นั่นเอง ปัญหานี้เกิดจากใน Linux จะมี umask ซึ่งเป้นเครื่องมือในการ mask permission ก่อนการเขียนไฟล์หรือโฟลเดอร์ใน Linux นั่นเอง ซึ่งเราสามารถดูได้ว่าปัจจุบัน umask ในเครื่องเป็นเท่าไรเพียงรันคำสั่ง umask ก็จะเห็น โดยส่วนใหญ่จะเป็น 022 ซึ่งเมื่อนำไป mask กับ 777 ก็จะได้เป็น 777 - 022 = 755 นั่นเอง วิธีการแก้ไขสามารถทำได้โดยก่อนสร้างไฟล์หรือโฟลเดอร์ให้ทำการเปลี่ยน umask ให้เป็น 0  แล้วจึงใช้คำสั่งเขียนไฟล์หรือโฟลเดอร์นั่นเอง เช่น


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
   $old = umask(0);
   chmod("/path/some_dir/some_file.txt", 0777);
   umask($old);

   // Checking
   if ($old != umask()) {
      die('An error occurred while changing back the umask');
   }
?>


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
   $old = umask(0);
   mkdir("/path/to/my/dir", 0777);
   umask($old);

   // Checking
   if ($old != umask()) {
      die('An error occurred while changing back the umask');
   }
?>

Comments

Popular posts from this blog

Serialize and Deserialize JSON data with C#

This post is about example for serialize and deserialize json data with C#. This example using Newtonsoft.Json to be a library to work with json data. You can add Newtonsoft.Json by download from  https://www.newtonsoft.com/json  or using Nuget to add it into your project. In this example is Bill object that hold billing data about Car object. Serialize Object to String Below is example code to serialize object to json string. At line 39 is code to convert object into json string with beautiful json format. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace JSON { class JSonExample { static void Main( string [] args) { List<Car> cars = new List<Car>(); float to...

How to clear Oracle Alert Log and Trace files using ADRCI command?

Sometime you will face the problem about large alert log and trace files in Oracle database. You can use ADRCI tool to manage them. Run adrci command [oracle@dboraclel1 dboraclel1]$ adrci ADRCI: Release 12.2.0.1.0 - Production on Tue Aug 21 00:20:25 2018 Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved. ADR base = "/u01/app/oracle" adrci> show home ADR Homes: diag/rdbms/schooldb/schooldb diag/tnslsnr/dboraclel1/listener diag/tnslsnr/dboraclel1/listener6 diag/tnslsnr/dboraclel1/listener2 diag/tnslsnr/dboraclel1/listener3 diag/tnslsnr/dboraclel1/listener4 diag/tnslsnr/dboraclel1/listener5 diag/diagtool/user_oracle/adrci_3644429138_107 adrci> Set home directory to work. If you have only one home folder skip this step. set homepath diag/tnslsnr/dboraclel1/listener Run this command to purge alert log. PURGE -age 1440 -type ALERT This command will be purge alert log older than 1440 minutes. Parameter age i...

Laravel 5.6.34 Released

We’ve had an exciting week in Laravel-land, with the launch of Laravel Nova on Wednesday! Tuesday’s release of Laravel 5.6.34 released quietly before Nova, bringing a few changes and a URL validation compatibility fix for PHP 7.3. First up, the whereRowValues changes to properly wrap columns: $builder = DB::table('table')->whereRowValues(['column1', 'column2'], '=', ['foo', 'bar']); dd($builder->toSql()); // expected: select * from "table" where ("column1", "column2") = (?, ?) // actual: select * from "table" where (column1, column2) = (?, ?) Next, the default mail template’s copyright phrase (i.e., “All rights reserved.”) is localizable. Here’s the implementation: @lang('All rights reserved.') The last change was updating the behavior of the EventFake to dispatch non-faked events. The previous functionality was not to execute any event listeners. Now, al...