CategoryEssentials

Create a CSV File for a user in PHP

C

problem statement If you have data in a MySQL database. You are sending the user a URL to get their data out as a CSV file. How can you, when they click the link, have a pop-up to download a CVS with the record from MySQL? solution 1 header("Content-Type: text/csv"); header("Content-Disposition: attachment; filename=file.csv"); function outputCSV($data) { $output =...

Print array to a file

P

problem statement If you want to print an array to a file, follow the solutions provided below. solution 1 Either var_export or set print_r to return the output instead of printing it. You can then save $results with file_put_contents. Or return it directly when writing to file: file_put_contents('filename.txt', print_r($b, true)); solution 2 Try following code. $h = fopen('filename...

Download File to server from URL

D

All you have to do to download a file to your server is by following the solutions provided below. solution 1 Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter: file_put_contents("Tmpfile.zip", fopen(";, 'r')); solution 2 private function downloadFile($url, $path) { $newfname = $path; $file = fopen ($url...

How can I find the php.ini file used by the command line?

H

If you need to enable pdo_mysql in the EasyPHP environment, follow the steps below. Just run php --ini and look for Loaded Configuration File in the output for the location of php.ini used by your CLI. solution 2 Unlike phpinfo() it will tell if it didn’t find/use a php.ini at all. var_dump( get_cfg_var('cfg_file_path') ); And you can simply set the location of the php.ini...

How to get a file’s extension in PHP?

H

This is a question you can read everywhere on the web with various answers: solution 1 People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP. In fact, it does exist, but few people know it. Meet pathinfo(): $ext = pathinfo($filename, PATHINFO_EXTENSION); This is fast and built-in. pathinfo() can give you other information...

Change the maximum upload file size

C

problem statement If you have a website hosted on a PC and you have no access to. You have an upload form allowing people to upload mp3 files up to 30MB big. Your server-side script is done in PHP. Every time you try to upload a file, receive an error claiming that the file exceeds the maximum size allowed, so need to increase the size. solution 1 You need to set the value...

private file handling with laravel

p

problem statement When you store files like pictures, videos, or other files, yo do not store them in the public folder but in a private folder that is not publicly accessible. Because only certain users should have access to the files. How can you output multiple images from a private folder? solution 1 To speed up you could do it via plain PHP (headers + read file): header('Content-Type:...

File Handling using PHP

F

This solution will guide the method to modify/delete the contents of a text file using PHP. solution 1. Using file_put_contents: file_put_contents($filename, 'file_content'); If you want to append to the file instead of replacing its contents use: file_put_contents($filename, 'append_this', FILE_APPEND); (file_out_contents is the simpler alternative to using the...

Upload a file using PHP

U

If you want to upload a file to a given folder, follow the solutions given below. solution 1 Below is one way to upload files, there are many other ways. <?php $target_dir = "upload/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); // Check if image file is a...

PHP file handling read and write

P

In this article, we will figure out how file handling works. problem statement Your task is to write a PHP script, which reads grades from the file “grades.txt”, raises all of them by one, writes the raised grades into the file “results.txt”, and finally prints the raised grades from the file “results.txt“. If a grade is 5, it won’t be raised. In grades...

Tutor Network

Learn PHP from A to Z