アプリ関連ニュース

Creating downloadable CSV file in PHP

This time I would like to share you how to automatically download CSV file using PHP.

To download CSV file, the header() function and the php://output parameter must be used.

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=myCSV.csv');
header("Content-Transfer-Encoding: UTF-8");

Setting the header is importance when downloading files automatically. In the first line of code, the content-type option is that HTTP header telling the browser that the MIME type of the content is application/csv.

Other particularly important option is in the second line of code, the filename option. This is where we want to name the file that will be generated and downloaded.

The other importance line of code is content transfer encoding ,this encoding method will transform binary email message data into the US-ASCII plain text format.

For populating CSV, fopen() giving us access to PHP’s output buffer. This line of code is that the file pointer connected to the output stream.

$f = fopen('php://output', 'a'); 

Writing data to the Csv and we can define column headings like this:

fputcsv($f, array('Column 1', 'Column 2', 'Column 3'));

We can fetch data and loop over the rows of data and output them.

mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$rows = mysql_query('SELECT field1,field2,field3 FROM table');

while ($row = mysql_fetch_assoc($rows)) fputcsv($f, $row);

By Ami



OCULUS AIR LINKが発表されました

FaceBookは近日リリース予定のv28アップデートにて、同社のVRヘッドセットOculusQuest2に新機能「OCULUS AIR LINK」が搭載されることを発表しました。

続きを読む

Unrar for macOS

Today I will talk about unrar for the people who are using macOS , can’t open rar file as default. For the zip file , we don’t need extra installable libraries or something as the system includes by default.

Recently, we can install unrar through brew like this.

brew install unrar

Unfortunately unrar has been removed from homebrew core because of licensing issues.

But don’t worry if you are looking for an alternatives, there is a library called carlocab/personal/unrar and you can install like this.

brew install carlocab/personal/unrar

After installation, we can call unrar in your terminal and it will look like this

and we can simply start using like this.

unrar x filename.rar

Actually, there are other formulae on homebrew-core that also extract various forms of compressed files. Depending on the formats you are working with, you may find that there is already an existing homebrew-core formula that suits your needs.

I will also add a link for your further reference here.

Thanks for reading.

Yuuma



Prohibited Validation Rules in Laravel

Sometimes we wish to forcefully prevent some of the fields in the request.

Now we just got that. Laravel 8 has three validation added for prohibited fields. Let’s walk through a few examples of where the prohibited validation rules might be useful and look at each one in more detail.

Prohibited Validation Rule

The “prohibited” rule checks if the field under validation must be empty or not present or throw an error otherwise.

// PUT /api/licenses/123-456

// {"name":"hello-world", "key":"random-key"}

$validated = $request->validate([
    'name' => 'required|max:255',
    'key' => 'prohibited',
]);
			
// Response: 422

// The key field is prohibited

The above is where a user might expect to update an API key by sending a PUT request to a resource. That field is likely ignored during the request. However, a successful response might lead to the user to believe they were able to update the key when in reality, the API ignored it.

If  prohibited validation rule registers, the key field is present in the request it will terminate the request with 422 HTTP code response code  and throw the error with the message The key field is prohibited.

Prohibited If and Unless Rules

The “prohibited_if ” rule checks if the field under validation must be empty or not present if the another field is equal to any value.

Validator::validate([
‘is_minor’ => true,
‘tos_accepted’ => true
], [
‘tos_accepted’ => ‘prohibited_if:is_minor, true’
]);

The above example is we’re expecting tos_accepted field will be forbidden if is_minor filed accepts true value. This example means someone accepting terms of service that has identified as a minor. Perhaps the application requires a parental registration to consent on their behalf.

The basic idea of “Prohibited_unless” rule is that a given filed should be prohibited from having data unless another field is equal to any value.

Validator::validate([
‘is_deceased’ => false,
‘date_of_death’ => ‘2021-03-09’
], [
‘date_of_death’ => ‘prohibited_unless:is_deceased,true’
]);

The above example might be initially the is_deceased filed set to false value.If we’re expecting the date_of_death field not to be forbidden, the is_deceased’ field set to true value.The example  illustrates perfectly how to use this rule to explicitly prevent contradictory input.

By Ami



[Unity] 3D Textの使用方法

今回はUnityの3D Objectの「3D Text」の使用方法をシェアします。
「3D Text」を使用すると、オブジェクトにテキストを配置したり、
テキスト内容や表示色等を動的に更新することができます。

続きを読む

アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム