Hướng dẫn readfile alternative php

I host my site on a shared hosting, which lately changed the server to safe mode (without even notifying that). I use a feature that download files from the server, using the readfile() function (I use php). Now, in safe_mode, this function is no longer available. Is there a replacement or a workaround to handle the situation that the file will be able to be downloaded by the user?

Thanks

asked Jun 13, 2011 at 10:28

Maor BarazanyMaor Barazany

7412 gold badges11 silver badges20 bronze badges

9

As I wrote in comments, readfile() is disabled by including it in disable_functions php.ini directive. It has nothing to do with safe mode. Try checking which functions are disabled and see if you can use any other filesystem function(-s) to do what readfile() does.

To see the list of disabled functions, use:

var_dump(ini_get('disable_functions'));

You might use:

// for any file
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    fpassthru($file);
    fclose($file);
}

// for any file, if fpassthru() is disabled
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    while ( !feof($file) ) {
        echo fread($file, 4096);
    }
    fclose($file);
}

// for small files;
// this should not be used for large files, as it loads whole file into memory
$data = file_get_contents($filename);
if ( $data !== false ) {
    echo $data;
}

// if and only if everything else fails, there is a *very dirty* alternative;
// this is *dirty* mainly because it "explodes" data into "lines" as if it was
// textual data
$data = file($filename);
if ( $data !== false ) {
    echo implode('', $data);
}

answered Jun 13, 2011 at 12:20

2

I assume you are using readfile to load remote files, as you said "from the server". If that is correct, your problem is not safe mode but that opening URLs with normal php file functions is not permitted anymore (setting allow_url_fopen disabled).

In that case, you can use PHP's curl functions to download files. Also, file_get_contents is a valid alternative.

answered Jun 13, 2011 at 10:53

cweiskecweiske

29k13 gold badges127 silver badges189 bronze badges

1

Hướng dẫn readfile alternative php

Base64_encode and base64_decode in php

(PHP 4, PHP 5, PHP 7, PHP 8)base64_encode — Encodes data with MIME base64Descriptionbase64_encode(string $string): string This encoding is designed to make binary data survive transport through ...

Hướng dẫn readfile alternative php

Best book for php beginners pdf

We are reader supported and may earn a commission when you buy through links on our sitePHP is a server-side scripting language used to develop static and dynamic websites or web applications. PHP ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng tostr php trong PHP

Bài này chúng ta sẽ tập trung tìm hiểu về 2 magic methods là __toString và invoke() trong PHP.1, __toString().-Phương thức __toString() sẽ được gọi khi chúng ta dùng ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng mailparser trong PHP

Theo phương thức truyền thống thì khi lập trình viên mobile khi tạo ra 1 ứng dụng mà cần liên quan đến dữ liệu thì sẽ cần phải kèm theo một web developer để ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

How to install simplexml php extension windows

Im trying to install laravel-excel on my laravel project. That library requires below php extensions to be enabled.PHP extension php_zipPHP extension php_xmlPHP extension php_gd2PHP extension ...

Hướng dẫn readfile alternative php

Hướng dẫn php oauth2-client example

Vietnamese (Tiếng Việt) translation by Andrea Ho (you can also view the original English article) Nội dung chínhAuth0 là gì?Tích hợp xác thực phía máy chủThiết lập dự ...

Hướng dẫn readfile alternative php

Where is php exe on windows?

Im currently developing a couple of plugins for Sublime Text 2 on OS X and I would like to make them cross platform, meaning I have to find out if and where php.exe is installed.Right now I call ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn dùng code mail trong PHP

Nội dung1. Hàm mail PHP là gì?2. Các tùy chọn gửi mail PHPCú pháp mail (Email Syntax)Tham số Email (Email Parameters)To:Subject:Message:Headers:Parameters:3. Lý do nên dùng mail ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng push shift trong PHP

Hàm array_push[] trong PHP dùng để thêm một phần tử mới vào cuối mảng, ví dụ mảng bạn có 10 phần tử thì hàm này sẽ thêm phần tử vào vị trí thứ 11, ...

Hướng dẫn readfile alternative php

Php echo array comma separated

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleThe comma separated list can be created by using implode() function. The implode() is a builtin function ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng in string trong PHP

1) Chuỗi là gì !?- Chuỗi (hay còn gọi được là chuỗi ký tự) là một dãy các ký tự.- Ví dụ: Tai Chuỗi gồm 3 ký tự Tai lieu Chuỗi gồm 8 ký tự Tai lieu ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn dùng sqrt sign trong PHP

Hàm toán học PHPPHP có một tập hợp các hàm toán học cho phép chúng ta thực hiện các nhiệm vụ toán học trên các con số.Nội dung chínhHàm toán học PHPHàm pi() ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng over functions trong PHP

Overloading in PHP provides means to dynamically create properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. The ...

Hướng dẫn readfile alternative php

Php create key value array in loop

I have this foreach loop:foreach($aMbs as $aMemb){ $ignoreArray = array(1,3); if (!in_array($aMemb[ID],$ignoreArray)){ $aMemberships[] = array($aMemb[ID] => $aMemb[Name]); ...

Hướng dẫn readfile alternative php

Php remove substring from end of string

I need to remove a substring of a string, but only when it is at the END of the string.for example, removing string at the end of the following strings :this is a test string -> this is a ...

Hướng dẫn readfile alternative php

Add key and value to array php

I was just looking for the same thing and I realized that, once again, my thinking is different because I am old school. I go all the way back to BASIC and PERL and sometimes I forget how easy things ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn scope php

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For ...

Hướng dẫn readfile alternative php

Php 8.1 disable deprecated warnings

I want to supress warning from this trigger_error(Deprecated, E_USER_DEPRECATED); in runtime. From I have read I can use error_reporting(E_ALL & -E_USER_DEPRECATED & -E_DEPRECATED);. But ...

Hướng dẫn readfile alternative php

How can i get days between two dates in php?

TL;DR do not use UNIX timestamps. Do not use time(). If you do, be prepared should its 98.0825% reliability fail you. Use DateTime (or Carbon).The correct answer is the one given by Saksham Gupta ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng png create trong PHP

Tổng quan về kỹ thuật Upload file PHPTạo form HTML fileCode Php xử lý uploadCode Php xử lý upload nhiều fileTổng quan về kỹ thuật Upload file trong PHPĐể HTML FORM có ...

Hướng dẫn readfile alternative php

How can we create a database using php and mysql

A database consists of one or more tables. You will need special CREATE privileges to create or to delete a MySQL database.Create a MySQL Database Using MySQLi and PDOThe CREATE DATABASE statement is ...

Hướng dẫn readfile alternative php

How do i get php on visual studio?

Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add ...

Hướng dẫn readfile alternative php

Hướng dẫn run bash in php

I have a bash script, that I run like this via the command line:./script.sh var1 var2 I am trying to execute the above command, after I call a certain php file.What I have right now is:$output = ...

Hướng dẫn readfile alternative php

Hướng dẫn structure of php w3schools

Learn PHPPHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.PHP is a widely-used, free, and efficient alternative to competitors such as Microsofts ...

Hướng dẫn readfile alternative php

Hướng dẫn chỉnh sửa file php

Nếu bạn đã từng sử dụng qua một số CMS như WordPress sẽ có chức năng chỉnh sửa giao diện (Theme Editor) cho phép can thiệp vào bên trong các đoạn mã. Với ...

Hướng dẫn readfile alternative php

Php remove last 2 characters from string

How can I remove three characters at the end of a string in PHP?abcabcabc would become abcabc! asked Feb 6, 2011 at 20:051Just do:echo substr($string, 0, -3); You dont need to use a strlen ...

Hướng dẫn readfile alternative php

How to calculate total hours in php

I have a small issue while calculating sum of total hoursMy working hours is like:Day1 - 12:23 Hours Day2 - 11:43 Hours Day3 - 10:18 Hours How can I get it by php? Thanks in advance Noich23.7k15 gold ...

Hướng dẫn readfile alternative php

How can i download php version in xampp?

XAMPP for Windows 7.4.29, 8.0.19 & 8.1.6 VersionChecksumSize7.4.29 / PHP 7.4.29 Whats Included? md5sha1 Download (64 bit) 159 Mb 8.0.19 / PHP 8.0.19 Whats Included? md5sha1 Download (64 ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn explode array in php

Khi học lập trình PHP bạn sẽ gặp rất nhiều hàm liên quan đến chuỗi và mảng. Hôm nay mình sẻ một hàm cũng thường được sử dụng khi sử dụng PHP đó là ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn reset trong php

Cú pháp hàm reset() trong PHPHàm reset() trong PHP có cú pháp như sau:reset ( $array );Định nghĩa hàm reset() trong PHPHàm reset() thiết lập con trỏ nội tại về phần tử ...

Hướng dẫn readfile alternative php

Hướng dẫn dùng callable definition trong PHP

Callbacks can be denoted by the callable type declaration. Some functions like call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be ...

Hướng dẫn readfile alternative php

Hướng dẫn readfile alternative php

Hướng dẫn dùng images x trong PHP

Với mọi trang web đều phải có chức năng upload image lên đưa Database với mục đích thêm hình ảnh vào cơ sở dữ liệu MySQL. Bài viết này sẽ hướng dẫn bạn ...