PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

fpassthru> <fnmatch
Last updated: Fri, 10 Oct 2008

view this page in

fopen

(PHP 4, PHP 5)

fopenОткрывает файл или URL

Описание

resource fopen ( string $filename , string $mode [, bool $use_include_path [, resource $zcontext ]] )

fopen() закрепляет именованый ресурс, указанный в аргументе filename , за потоком. Если filename передан в форме "scheme://...", он считается URL'ом и PHP проведёт поиск обработчика протокола (также известного как "обвёртка") для этой схемы. Если ни одна обвёртка не закреплена за протоколом, PHP выдаст замечание чтобы помочь вам отследить потенциальную проблему в вашем скрипте и продолжит выполнение, будто filename указывает на обыкновенный файл.

Если PHP решил, что filename указывает на локальный файл, тогда он попытается открыть поток к этому файлу. Файл должен быть доступен PHP, так что вам следует убедиться, что права доступа на файл разрешают это. Если вы вкдючили безопасный режим или open_basedir, накладываются дальнейшие ограничения.

Если PHP решил, что filename указывает на зарегистрированный протокол и этот протокол зарегистрирован как сетевой URL, PHP проверит состояние директивы allow_url_fopen. Если она выключена, PHP выдаст предупреждение и вызов fopen закончится неудачей.

Замечание: Список поддерживаемых протоколов доступен в разделе List of Supported Protocols/Wrappers. Некоторые протоколы (обвёртки) поддерживают context и/или опции php.ini. Обратитесь к соответствующей странице с описанием используемого протокола для получения списка опций, которые могут быть установлены. (например, значение php.ini user_agent используется обвёрткой http). Для получения описания параметров contexts и zcontext , обратитесь к разделу Stream Функции.

Замечание: Поддержка контекста была добавлена в PHP 5.0.0.

Замечание: Начиная с версии PHP 4.3.2, бинарный режим является режимом по умолчанию для всех платформ, которые различают бинарный и текстовый, режимы. Если у вас возникли проблемы после обновления, попробуйте использовать флаг 't' в качестве обходного пути до тех пор, пока вы не измените свои скрипты для достижения большей портируемости, как отмечено выше.

Параметр mode указывает тип доступа, который вы запрашиваете у потока. Он может быть одним из следующих:

Список возможных режимов для fopen() используя mode
mode Описание
'r' Открывает файл только для чтения; помещает указатель в начало файла.
'r+' Окрывает файл для чтения и записи; помещяет указатель в начало файла.
'w' Открывает файл только для записи; помещает указатель в начало файла и обрезает файл до нулевой длинны. Если файл не существует - пробует его создать.
'w+' Открывает файл для чтения и записи; помещает указатель в начало файла и обрезает файл до нулевой длинны. Если файл не существует - пробует его создать.
'a' Открывает файл только для записи; помещает указатель в конец файла. Если файл не существует - пытается его создать.
'a+' Открывает файл для чтения и записи; помещает указатель в конец файла. Если файл не существует - пытается его создать.
'x' Создаёт и открывает только для записи; помещает указатель в начало файла. Если файл уже существует, вызов fopen() закончится неудачей, вернёт FALSE и выдаст предупреждение уровня E_WARNING. Если файл не существует, попытается его создать. Это эквивалентно указанию флагов O_EXCL|O_CREAT для внутреннего системного вызова open(2). Эта опция поддерживается начиная с версии PHP 4.3.2 и выше, и работает только для локальных файлов.
'x+' Создаёт и открывает для чтения и записи; помещает указатель в начало файла. Если файл уже существует, вызов fopen() закончится неудачей, вернёт FALSE и выдаст предупреждение уровня E_WARNING. Если файл не существует, попытается его создать. Это эквивалентно указанию флагов O_EXCL|O_CREAT для внутреннего системного вызова open(2). Эта опция поддерживается начиная с версии PHP 4.3.2 и выше, и работает только для локальных файлов.

Замечание: Разные семейства операционных систем имеют разные соглашения относительно окончанийя строк. Когда вы пишете текст и хотите вставить разрыв строки, вы должны использовать правильный(-ые) символ(ы) для вашей операционной системы. Системы семейства Unix используют \n в качестве символа конца строки, системы семейства Windows используют \r\n в качестве символов окончания строки и системы семейства Macintosh используют \r в качестве символа конца строки.
Если вы используете неверный символ конца строки при редактировании файлов, вы можете обнаружить, что при открытии эти файлы "смешно выглядят".
Windows предлагает флаг режима текстовой трансляции ('t'), который автоматически переведёт \n во время работы с файлом. И наоборот - также вы можете использовать 'b' чтобы принудительно включить бинарный (двоичный) режим, в котором ваши данные не будут преобразовываться. Чтобы использовать эти режимы, укажите 'b' или 't' в качестве последней буквы параметра mode .
Так как установка флага трансляции по умолчанию зависит от SAPI и версии PHP, которую вы используете, рекомендуем явно задавать указанный флаг из соображений портируемости. Вы должны использовать режим 't' если вы работаете с текстовым файлом и использовать \n для разделения для обозначения конца строки в вашем скрипте, при этом не беспокоясь за читаемость ваших файлов в других приложениях типа "Блокнота". В противном случае вам следует использовать флаг 'b'.
Если вы явно не укажете флаг 'b' во время работы с бинарными файлами, вы можете столкнуться со странной порчей ваших данных, включая испорченные файлы изображений и странные проблемы с символами \r\n.

Замечание: Из соображений портируемости, настоятельно рекомендуется всегда использовать флаг 'b' при открытии файлов с помощью fopen().

Замечание: Кроме того, из соображений портируемости, также настойчиво рекомендуется переписать старый код, который полагается на режим 't', чтобы вместо этого он использовал правильные окончания строк и режим 'b'.

Необязательный третий параметр use_include_path может быть установлен в '1' или TRUE, если вы также хотите провести поиск файла в include_path.

Если открыть файл не удалось, функция вернёт FALSE и сгенерирует ошибку уровня E_WARNING. Вы можете использовать @ для того, чтобы подавить это предупреждение.

Пример #1 Примеры использования функции fopen()

<?php
$handle 
fopen("/home/rasmus/file.txt""r");
$handle fopen("/home/rasmus/file.gif""wb");
$handle fopen("http://www.example.com/""r");
$handle fopen("ftp://user:password@example.com/somefile.txt""w");
?>

Если вы сталкиваетесь с проблемами во время чтения или записи файлов, и вы используете PHP в виде серверного модуля, убедитесь в том, что процесс сервера имеет доступ к файлам и директориям, которые вы используете.

На платформе Windows, вам необходимо не забывать экранировать все обратные слеши в пути к файлу или использовать прямые слеши.

<?php
$handle 
fopen("c:\\data\\info.txt""r");
?>

Внимание

При использовании SSL, Microsoft IIS нарушает протокол, закрывая соединение без отправки индикатора close_notify. PHP сообщит об этом как о "SSL: Fatal Protocol Error" в тот момент, когда вы достигнете конца данных. Чтобы обойти это, вы должны установить error_reporting на уровень, исключающий E_WARNING. PHP версий 4.3.7 и старше умеет определять, что на стороне сервера находится проблемный IIS и не выводит предупреждение. Если вы используете fsockopen() для создания ssl:// сокета, вы сами отвечаете за определение и подавление этого предупреждения.

Замечание: Когда опция safe mode включена, PHP проверяет, имеет ли каталог, с которым вы собираетесь работать, такой же UID (владельца), как и выполняемый скрипт.

См. также раздел List of Supported Protocols/Wrappers, описание функций fclose(), fgets(), fread(), fwrite(), fsockopen(), file(), file_exists(), is_readable(), stream_set_timeout() и popen().



fpassthru> <fnmatch
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
fopen
raboof
17-Sep-2008 12:57
I think it deserves a mention that if you fopen() a handle, you also have to obligation to fclose() it again.
Luc
27-Aug-2008 01:58
If your connection goes through a proxy, you may also
try using CURL:

curl_setopt($CurlHandler, $CURLOPT_PROXY, "192.168.0.2");
curl_setopt($CurlHandler, CURLOPT_PROXYUSERPWD, "login:password");
php at delhelsa dot com
24-Jun-2008 01:27
With php 5.2.5 on Apache 2.2.4, accessing files on an ftp server with fopen() or readfile() requires an extra forwardslash if an absolute path is needed.

i.e., if a file called bullbes.txt is stored under /var/school/ on ftp server townsville and you're trying to access it with user blossom and password buttercup, the url would be:

ftp://blossom:buttercup@townsville//var/school/bubbles.txt

Note the two forwardslashes. It looks like the second one is needed so the server won't interpret the path as relative to blossom's home on townsville.
erk_3 at hotmail dot com
12-May-2008 09:48
If you are getting permission denied trying to write/read to a network resource, you have to change the system account that the apache service is runnning on.
However if you are on a domain, you will need to use the user name as:
user@domain.com

If you user the format: domain\username  the service will successfully start, but you will still receive errors trying to access the network resource.
webmaster at myeshop dot fr
26-Apr-2008 08:40
Also a small function useful for backup for example. It's a mixed between the fopen() and the mkdir() functions.

This function opens a file but also make the path recursively where the file is contained. This is helpful for ending to finish with "No such file or directory in" errors

function fopen_recursive($path, $mode, $chmod=0755){
  preg_match('`^(.+)/([a-zA-Z0-9]+\.[a-z]+)$`i', $path, $matches);
  $directory = $matches[1];
  $file = $matches[2];

  if (!is_dir($directory)){
    if (!mkdir($directory, $chmod, 1)){
    return FALSE;
    }
  }
 return fopen ($path, $mode);
}
jabeba at web dot de
17-Apr-2008 07:21
If you have to use a proxy to make requests outside of your local network, you may use this class:

/*
 *
 * No Proxy Authentification Implemented; PHP 5
 *
 */

class RemoteFopenViaProxy {

    private $result;
    private $proxy_name;
    private $proxy_port;
    private $request_url;

    public function get_proxy_name() {
        return $this->proxy_name;
    }

    public function set_proxy_name($n) {
        $this->proxy_name = $n;
    }

    public function get_proxy_port() {
        return $this->proxy_port;
    }

    public function set_proxy_port($p) {
        $this->proxy_port = $p;
    }

    public function get_request_url() {
        return $this->request_url;
    }

    public function set_request_url($u) {
        $this->request_url = $u;
    }

    public function get_result() {
        return $this->result;
    }

    public function set_result($r) {
        $this->result = $r;
    }

    private function get_url_via_proxy() {

        $proxy_fp = fsockopen($this->get_proxy_name(), $this->get_proxy_port());

        if (!$proxy_fp) {
            return false;
        }
        fputs($proxy_fp, "GET " . $this->get_request_url() . " HTTP/1.0\r\nHost: " . $this->get_proxy_name() . "\r\n\r\n");
        while (!feof($proxy_fp)) {
            $proxy_cont .= fread($proxy_fp, 4096);
        }
        fclose($proxy_fp);
        $proxy_cont = substr($proxy_cont, strpos($proxy_cont, "\r\n\r\n") + 4);
        return $proxy_cont;

    }

    private function get_url($url) {
        $fd = @ file($url);
        if ($fd) {
            return $fd;
        } else {
            return false;
        }
    }

    private function logger($line, $file) {
        $fd = fopen($file . ".log", "a+");
        fwrite($fd, date("Ymd G:i:s") . " - " . $file . " - " . $line . "\n");
        fclose($fd);
    }

    function __construct($url, $proxy_name = "", $proxy_port = "") {

        $this->set_request_url($url);
        $this->set_proxy_name($proxy_name);
        $this->set_proxy_port($proxy_port);

    }

    public function request_via_proxy() {

        $this->set_result($this->get_url_via_proxy());
        if (!$this->get_result()) {
            $this->logger("FAILED: get_url_via_proxy(" . $this->get_proxy_name() . "," . $this->get_proxy_port() . "," . $this->get_request_url() . ")", "RemoteFopenViaProxyClass.log");
        }
    }

    public function request_without_proxy() {

        $this->set_result($this->get_url($this->get_request_url()));
        if (!$this->get_result()) {
            $this->logger("FAILED: get_url(" . $url . ")", "RemoteFopenViaProxyClass.log");
        }
    }
}

Use it this way:
// call constructor
$obj = new RemoteFopenViaProxy($insert_request_url, $insert_proxy_name, $insert_proxy_port);
// change settings after object generation
$obj->set_proxy_name($insert_proxy_name);
$obj->set_proxy_port($insert_proxy_port);
$obj->set_request_url($insert_request_url);
$obj->request_via_proxy();
echo $obj->get_result();

If there are errors during execution, the script tries to write some useful information into a log file.
jphansen at uga dot edu
22-Feb-2008 08:04
If you open a file with r+ and execute an fwrite(), writing less to the file than what it originally was, it will result in the difference being padded with the end of the file from the previous end of the file. Example:

<?
// Open file for read and string modification
$file = "/test";
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
$new_contents = str_replace("hello world", "hello", $contents);
fclose($fh);

// Open file to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_contents);
fclose($fh);
?>

If the end of the file was "abcdefghij", you will notice that the difference in "hello world" and "hello", 6 characters, will be appended to the file, resulting in the new ending: "efghij". To obviate this, fopen() with +w instead, which truncates the file to zero length.
sean downey
09-Feb-2008 01:23
when using ssl / https on windows i would get the error:
"Warning: fopen(https://cia.gov): failed to open stream: Invalid argument in someSpecialFile.php on line 4344534"

This was because I did not have the extension "php_openssl.dll" enabled.

So if you have the same problem, goto your php.ini file and enable it :)
richard dot thomas at psysolutions dot com
28-Nov-2007 12:22
fopen appears to choke on some redirects which do not give problems to browsers or wget

Example: this redirect

Location: /NetPerfMon/NetworkMap.asp?Map=Network Overview&Scale=50

Causes this request:

GET /NetPerfMon/NetworkMap.asp?Map=Network Overview&Scale=50 HTTP/1.0

Which is an invalid request due to the space. I don't recall if this is correct behavior according to the RFC or not (I'm 100% sure that fopen shouldn't be sending requests with spaces in but it may be that the Location: redirect shouldn't have spaces either) but the the big two browsers and wget handle it gracefully

I'll also add a note that fopen does no cookie handling. The above is part of two redirects. The first redirects to a page which issues cookies, the second redirects to the original page. If there was not a problem with the space above, this would have led to a loop broken only by any redirect limit rules coded into fopen. Handling cookies may be outside the scope of fopen but I think it is worth noting in case anyone else would run into it.

PHP 5.2.0
info at NOSPAMPLEASE dot c-eagle dot com
08-Oct-2007 07:14
If there is a file that´s excessively being rewritten by many different users, you´ll note that two almost-simultaneously accesses on that file could interfere with each other. For example if there´s a chat history containing only the last 25 chat lines. Now adding a line also means deleting the very first one. So while that whole writing is happening, another user might also add a line, reading the file, which, at this point, is incomplete, because it´s just being rewritten. The second user would then rewrite an incomplete file and add its line to it, meaning: you just got yourself some data loss!

If flock() was working at all, that might be the key to not let those interferences happen - but flock() mostly won´t work as expected (at least that´s my experience on any linux webserver I´ve tried), and writing own file-locking-functions comes with a lot of possible issues that would finally result in corrupted files. Even though it´s very unlikely, it´s not impossible and has happened to me already.

So I came up with another solution for the file-interference-problem:

1. A file that´s to be accessed will first be copied to a temp-file directory and its last filemtime() is being stored in a PHP-variable. The temp-file gets a random filename, ensuring no other process is able to interfere with this particular temp-file.
2. When the temp-file has been changed/rewritten/whatever, there´ll be a check whether the filemtime() of the original file has been changed since we copied it into our temp-directory.
2.1. If filemtime() is still the same, the temp-file will just be renamed/moved to the original filename, ensuring the original file is never in a temporary state - only the complete previous state or the complete new state.
2.2. But if filemtime() has been changed while our PHP-process wanted to change its file, the temp-file will just be deleted and our new PHP-fileclose-function will return a FALSE, enabling whatever called that function to do it again (ie. upto 5 times, until it returns TRUE).

These are the functions I´ve written for that purpose:

<?php
$dir_fileopen
= "../AN/INTERNAL/DIRECTORY/fileopen";

function
randomid() {
    return
time().substr(md5(microtime()), 0, rand(5, 12));
}

function
cfopen($filename, $mode, $overwriteanyway = false) {
    global
$dir_fileopen;
   
clearstatcache();
    do {
       
$id = md5(randomid(rand(), TRUE));
       
$tempfilename = $dir_fileopen."/".$id.md5($filename);
    } while(
file_exists($tempfilename));
    if (
file_exists($filename)) {
       
$newfile = false;
       
copy($filename, $tempfilename);
    }else{
       
$newfile = true;
    }
   
$fp = fopen($tempfilename, $mode);
    return
$fp ? array($fp, $filename, $id, @filemtime($filename), $newfile, $overwriteanyway) : false;
}

function
cfwrite($fp,$string) { return fwrite($fp[0], $string); }

function
cfclose($fp, $debug = "off") {
    global
$dir_fileopen;
   
$success = fclose($fp[0]);
   
clearstatcache();
   
$tempfilename = $dir_fileopen."/".$fp[2].md5($fp[1]);
    if ((@
filemtime($fp[1]) == $fp[3]) or ($fp[4]==true and !file_exists($fp[1])) or $fp[5]==true) {
       
rename($tempfilename, $fp[1]);
    }else{
       
unlink($tempfilename);
        if (
$debug != "off") echo "While writing, another process accessed $fp[1]. To ensure file-integrity, your changes were rejected.";
       
$success = false;
    }
    return
$success;
}
?>

$overwriteanyway, one of the parameters for cfopen(), means: If cfclose() is used and the original file has changed, this script won´t care and still overwrite the original file with the new temp file. Anyway there won´t be any writing-interference between two PHP processes, assuming there can be no absolute simultaneousness between two (or more) processes.
misc at n4te dot com
05-Oct-2007 07:21
The UTF-8 BOM is optional. PHP does not ignore it if it is present when reading UTF-8 encoded data. Here is a function that skips the BOM, if it exists.

// Reads past the UTF-8 bom if it is there.
function fopen_utf8 ($filename, $mode) {
    $file = @fopen($filename, $mode);
    $bom = fread($file, 3);
    if ($bom != b"\xEF\xBB\xBF")
        rewind($file, 0);
    else
        echo "bom found!\n";
    return $file;
}
bluej100@gmail
20-Aug-2007 04:28
@ericw at w3consultant dot net:

It's a resource, not an object. var_dump meant what it said. ;)

http://us.php.net/manual/en/language.types.resource.php

http://us.php.net/manual/en/function.is-resource.php
Adlez
04-Aug-2007 02:32
For those of you who do not have cURL, you might want to try this.
It doesn't have all the functions that cURL has, but it has the basics.
Please let me know of any bugs or problems.

<?php
function open_page($url,$f=1,$c=2,$r=0,$a=0,$cf=0,$pd=""){
 global
$oldheader;
 
$url = str_replace("http://","",$url);
 if (
preg_match("#/#","$url")){
 
$page = $url;
 
$url = @explode("/",$url);
 
$url = $url[0];
 
$page = str_replace($url,"",$page);
  if (!
$page || $page == ""){
  
$page = "/";
  }
 
$ip = gethostbyname($url);
 }else{
 
$ip = gethostbyname($url);
 
$page = "/";
 }
 
$open = fsockopen($ip, 80, $errno, $errstr, 60);
 if (
$pd){
 
$send = "POST $page HTTP/1.0\r\n";
 }else{
 
$send = "GET $page HTTP/1.0\r\n";
 }
 
$send .= "Host: $url\r\n";
 if (
$r){
 
$send .= "Referer: $r\r\n";
 }else{
  if (
$_SERVER['HTTP_REFERER']){
  
$send .= "Referer: {$_SERVER['HTTP_REFERER']}\r\n";
  }
 }
 if (
$cf){
  if (@
file_exists($cf)){
  
$cookie = urldecode(@file_get_contents($cf));
   if (
$cookie){
   
$send .= "Cookie: $cookie\r\n";
   
$add = @fopen($cf,'w');
   
fwrite($add,"");
   
fclose($add);
   }
  }
 }
 
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
 if (
$a){
 
$send .= "User-Agent: $a\r\n";
 }else{
 
$send .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
 }
 if (
$pd){
 
$send .= "Content-Type: application/x-www-form-urlencoded\r\n"
 
$send .= "Content-Length: " .strlen($pd) ."\r\n\r\n";
 
$send .= $pd;
 }else{
 
$send .= "Connection: Close\r\n\r\n";
 }
 
fputs($open, $send);
 while (!
feof($open)) {
 
$return .= fgets($open, 4096);
 }
 
fclose($open);
 
$return = @explode("\r\n\r\n",$return,2);
 
$header = $return[0];
 if (
$cf){
  if (
preg_match("/Set\-Cookie\: /i","$header")){
  
$cookie = @explode("Set-Cookie: ",$header,2);
  
$cookie = $cookie[1];
  
$cookie = explode("\r",$cookie);
  
$cookie = $cookie[0];
  
$cookie = str_replace("path=/","",$cookie[0]);
  
$add = @fopen($cf,'a');
  
fwrite($add,$cookie,strlen($read));
  
fclose($add);
  }
 }
 if (
$oldheader){
 
$header = "$oldheader<br /><br />\n$header";
 }
 
$header = str_replace("\n","<br />",$header);
 if (
$return[1]){
 
$body = $return[1];
 }else{
 
$body = "";
 }
 if (
$c === 2){
  if (
$body){
  
$return = $body;
  }else{
  
$return = $header;
  }
 }
 if (
$c === 1){
 
$return = $header;
 }
 if (
$c === 3){
 
$return = "$header$body";
 }
 if (
$f){
  if (
preg_match("/Location\:/","$header")){
  
$url = @explode("Location: ",$header);
  
$url = $url[1];
  
$url = @explode("\r",$url);
  
$url = $url[0];
  
$oldheader = str_replace("\r\n\r\n","",$header);
  
$l = "&#76&#111&#99&#97&#116&#105&#111&#110&#58";
  
$oldheader = str_replace("Location:",$l,$oldheader);
   return
open_page($url,$f,$c,$r,$a,$cf,$pd);
  }else{
   return
$return;
  }
 }else{
  return
$return;
 }
}
/////////////
////Usage////
/////////////
$url = "http://www.php.net";
$f = 1;
$c = 2;//1 for header, 2 for body, 3 for both
$r = NULL;
$a = NULL;
$cf = NULL;
$pd = NULL;
$page = open_page($url,$f,$c,$r,$a,$cf,$pd);
print
$page;
?>
Elliott Brueggeman
02-Jul-2007 12:43
I did some basic performance testing of fopen() + fread() versus file_get_contents() when opening a remote file for reading, and it looks like fread() is actually a little bit faster than fopen(). While it may be because of my GoDaddy hosting server’s configuration, it makes me favor using fopen() over file_get_contents(). Fopen() also has the added benefit of greater PHP version compatibility.

Test results: http://www.ebrueggeman.com/php_benchmarking_fopen.php
JB
07-Jun-2007 04:30
A note on pflaume dot NOSPAM at NOSPAM dot gmx dot de's proxy application for fopen, which was very handy.
I found with Tor on OSX that the Host request header to the proxy (set to the Proxy IP) was not necessary, and was passed on to the target site causing most to return a 404 Error.
simon dot allen at swerve dot co dot nz
09-Apr-2007 08:56
using fopen to upload a file through ftp cannot overwrite that file - use curl instead
naidim at gmail dot com
28-Mar-2007 04:54
While PHP does not have a function to insert text into the middle of a file, it is not that complicated to do.

<?php
function addRSSItem($rssFile, $firstItem, $item){
   
// Backup file
   
if(!copy($rssFile, 'backup.rss')) die('Backup failed!');
   
// Store file contents in array
   
$arrFile = file($rssFile);
   
// Open file for output
   
if(($fh = fopen($rssFile,'w')) === FALSE){
        die(
'Failed to open file for writing!');
    }
   
// Set counters
   
$currentLine = 0;
   
$cntFile = count($arrFile);
   
// Write contents, inserting $item as first item
   
while( $currentLine <= $cntFile ){
        if(
$currentLine == $firstItem) fwrite($fh, $item);
       
fwrite($fh, $arrFile[$currentLine]);
       
$currentLine++;
    }
   
// Delete backup
   
unlink('backup.rss');
}

$data = "    <item>\n<title>$_POST['title]</title>\n".
 
"        <description>$_POST['description']</description>\n".
 
"        <pubDate>$_POST['date']</pubDate>\n".
 
"        <link>http://www.site.com/mp3s/".
 
basename($_FILES['fullPath']['name'])."</link>".
 
"        <enclosure url=\"http://www.site.com/mp3s/".
 
basename($_FILES['fullPath']['name']).
 
"\" length=\"$_FILES[fullPath][size]\" type=\"audio/mpeg\" />".
 
"    </item>\n";
addRSSItem('/var/www/html/rss/podcast.rss',20,$data);
?>
20-Mar-2007 06:45
If you want to download a file from a URL to a local file, you could just as well use copy() :)
andrew at NOSPAM dot neonsurge dot com
09-Feb-2007 07:22
My recent findings on high-performance fopen/fsockopen usage.

Note #1: The performance comparison below regarding curl is obsolete when utilizing certain things in this comment.  My performance tests download and upload about 97% as fast as curl with a custom non-socket blocking HTTP Transport class written for a high performance system in PHP5.

Note #2: fopen and fsockopen have a "feature' that always forces DNS resolution.  Check this code...

<?php for ($i = 0; $i < 50; $i++) {
   
$errno = $errstr = "";
   
//$ip = gethostbyname("php.net");  $a = fsockopen($ip,22,$errno,$errstr,10);  //FAST way
   
$a = fsockopen("php.net",22,$errno,$errstr,10); //SLOW way
   
$ab = fread($a,4096); unset($a, $ab);
}
?>

fsockopen() and fopen() always force php.net to be resolved every time and in this example above it resolves the name 50 seperate times and does not use the local cache.  To get around this, gethostbyname() does use your local DNS cache properly, it will not try to get the IP from your DNS server 50 times.  The above code for me to a personal server took 87 seconds the fast way, and 5.74 seconds the slow way, a 650% increase.  And this is single-threaded!  ;)

Note #3: I see a lot of notes and people mentioning non-blocking sockets, especially for HTTP transport.  I thought I would share a little from my experience.  First, the above command fsockopen() allows you to specify a timeout, after you check if it's  opened properly (as you should _always_) you just need to...

<?php stream_set_blocking($a,0);?>

From this point on certain considerations must be taken.  Remember you are not blocking anymore, so when you want to write or read a lot of data it will always return to you instantly.  Which is important since you need to check the return value of your writes and reads against how much you expect to read/write.  For reading if you do not know how long it is, checking for EOF works also.

This is in fact a neat feature and state, since you can now make a read/write loop to send/receive a lot of data and check the time/timeout value(s) constantly.  If that timeout is hit you can throw back errors properly to whatever function/method/code called your transport function/class.  The graceful failure with custom shorter failure times allows your application to continue, especially web-based applications where fopen alone and even curl under certain circumstances does not follow your requested timeouts, it will wait a full 60-90 seconds, depending on your OS.

Good ways to test a custom non-blocking timeout supported transport method described above is to make one first, and then transfer a large file with it, and halfway through unplug your network cable.  Curl or fopen/fread/fwrite alone will croak and make your applications wait a full 60-90 seconds, whereas a nice custom class will check if no data has been transferred for 15 seconds (or less!) and will fail gracefully with a error.

If anyone is interested in chatting about this feel free to contact me or add to this comment.
download dot function at nospam dot com
22-Jan-2007 03:34
Thanks very much to flobee (15-Jan-2006) who provided a download() function with realtime writing instead of buffering the result, very clever. However, your function is too basic, it has almost zero error checking, and also forgets to close file handles in one case(!). Not only that, but you have mixed up the meaning of return values, where you have made false = no errors and true = error occured. I've added full error checking, optimized the code, and of course corrected the return values. True = Success. Here are the changes:

1. If the file that we are saving to already exists it removes any read-only flag so that it can be overwritten.
2. Correctly formats the URL we are downloading from so that fopen() understands it. For instance, if you pass a URL with spaces, those have to be encoded to %20, and any html entities such as &amp; have to be decoded to & or the fopen() call fails, I've added these transformations so that the user does not have to pre-format the URL before passing it to download().
3. I've added the closing of file handles at a point where you forgot to.
4. The error messages are all handled by fopen/fwrite, so if the function has returned false for an error, you need to enable "Display_Errors" in the INI to see what happened, or alternatively use "ini_set('display_errors', 1);" at the top of your PHP file.

I don't think there is _anything_ else that can be improved, but if you can think of something then you are welcome to submit your version.

function download ($file_source, $file_target)
{
  // Preparations
  $file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format
  if (file_exists($file_target)) { chmod($file_target, 0777); } // add write permission

  // Begin transfer
  if (($rh = fopen($file_source, 'rb')) === FALSE) { return false; } // fopen() handles
  if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; } // error messages.
  while (!feof($rh))
  {
    // unable to write to file, possibly because the harddrive has filled up
    if (fwrite($wh, fread($rh, 1024)) === FALSE) { fclose($rh); fclose($wh); return false; }
  }

  // Finished without errors
  fclose($rh);
  fclose($wh);
  return true;
}
perrog at gmail dot com
21-Jan-2007 05:05
Note: If you have opened the file in append mode ("a" or "a+"), any data you write to the file will always be appended, regardless of the file position. But PHP distinguish between read and write position, and you may freely read at any position, but when you write it will always append at the end.

If you don't want that write restriction, open the file in read-write mode ("r+") and then start by moving the file pointer to the end.

if (($fp = fopen($filename, "r+") === FALSE) {
  // handle error
  exit;
}

if (fseek($fp, 0, SEEK_END) === -1) {
  // handle error
  exit;
}
patryk dot szczyglowski at gmail dot com
20-Sep-2006 08:02
Watch out not to specify empty string as filename. It seems PHP is trying to get data from stdin which may end up in script timeout. It may not be trivial to find.

<?php
$fp
= fopen('', 'r'); // wrong
?>
roman dot nastenko at gmail dot com
19-May-2006 09:09
php 4.4.2 realy introduced a blocker problem with fopen() (http://bugs.php.net/bug.php?id=36017)

In that case you can use sockets for file open.

Example. Not

   $viart_xml = fopen("http://www.viart.com/viart_shop.xml", "r");

But

   $viart_xml = fsockopen("www.viart.com", 80, $errno, $errstr, 12);

   fputs($viart_xml, "GET /viart_shop.xml HTTP/1.0\r\n");
   fputs($viart_xml, "Host: www.viart.com\r\n");
   fputs($viart_xml, "Referer: http://www.viart.com\r\n");
   fputs($viart_xml, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n");

After that you can use $viart_xml as a simple file :)
puneet at puneetarora dot com
03-May-2006 04:25
Refering to the note by [info at b1g dot de]
on (24-Oct-2005 11:54)

sometimes you may also want HTTP error code returned from the server. The code of the HTTPRequest class can be modified as followed to do so ..

1. Add another member variable to the class

var $_error;       // HTTP Error code

2. After  the lines
.
.
       // parse headers
       $headers = array();
       $lines = explode($crlf, $header);
.
.

add

list($proto, $this->_error, $reply) = explode(" ", $lines[0]);

$proto and $reply can be treated as junk variables.

once DownloadToString() is called, the HTTP error code will be contained in the _error property of the object.
ceo at l-i-e dot com
10-Apr-2006 09:13
If you need fopen() on a URL to timeout, you can do like:
<?php
  $timeout
= 3;
 
$old = ini_set('default_socket_timeout', $timeout);
 
$file = fopen('http://example.com', 'r');
 
ini_set('default_socket_timeout', $old);
 
stream_set_timeout($file, $timeout);
 
stream_set_blocking($file, 0);
 
//the rest is standard
?>
zerkella at mi6 dot com dot ua
05-Apr-2006 08:02
During development of a set of non-blocking functions for downloading files from http-servers I've discovered that it's not possible to set timeout for fopen('http://somesite.com/somefile', 'rb').

All functions for controlling non-blocking mode (stream_set_blocking, stream_set_timeout, stream_context_set_option) use resource handle that is created via fopen(). But fopen() for HTTP connections internally makes quite a big set of actions: it creates socket, resolves webserver name, establishes actual connection. So hanging can occur anywhere in resolving and creating tcp-connection but you cannot control it.

Solutions:
1) Use socket functions. Set socket in non-blocking mode just after creation. Implement HTTP-protocol yourself. In source code use these manually created functions.
2) Write a wrapper for myprotocol://, which internally will use first solution, but in source code you'll use fopen('myprotocol://somesite.com/somefile', 'rb') with some way to set timeout before calling it.
Jeff McKenna
28-Feb-2006 02:26
It seems php 4.4.2 introduced a blocker problem with fopen() (http://bugs.php.net/bug.php?id=36017).
Paul Yanchenko
21-Jan-2006 06:16
I found a strange behaviour of PHP when several scripts trying to fopen a file for writing access simultaneously. I expect that this will result file sharing violation, but it's not and all scripts can open and write that file. I'd like to use sharing violation to control uniqueness of script instance, but now I even do know how to do this. The only idea is to use "x" flag in fopen(), but if that lock-file somehow will not be deleted - script will never run.

PHP 5.0.5, Windows XP Pro SP2
flobee
14-Jan-2006 08:58
download: i need a function to simulate a "wget url" and do not buffer the data in the memory to avoid thouse problems on large files:
<?php
function download($file_source, $file_target) {
       
$rh = fopen($file_source, 'rb');
       
$wh = fopen($file_target, 'wb');
        if (
$rh===false || $wh===false) {
// error reading or opening file
          
return true;
        }
        while (!
feof($rh)) {
            if (
fwrite($wh, fread($rh, 1024)) === FALSE) {
                  
// 'Download error: Cannot write to file ('.$file_target.')';
                  
return true;
               }
        }
       
fclose($rh);
       
fclose($wh);
       
// No error
       
return false;
    }
?>
anonymous at anonymous dot com
26-Dec-2005 04:11
Contrary to a note below the concept of what the preferred line ending is on mac os x is a little bit fuzzy.  I'm pretty sure all the bsd utils installed by default are going to use \n but that is not necessarily the norm.  Some apps will use that while others will use \r.

You should be prepared to deal with either.
Camillo
19-Dec-2005 10:58
Contrary to what this page says, the preferred line ending on Macintosh systems is \n (LF). \r was used on legacy versions of the Mac OS (pre-OS X), and I don't think PHP even runs on those.
151408626 dot unique at servux dot org
19-Dec-2005 05:05
I found a nice trick how to work around the issue (mentioned here: http://www.php.net/manual/en/function.fopen.php#41243) that the PHP  process will block on opening a FIFO until data is sent to it:

Simply do send some data to the FIFO, using an echo command started in background (and ignoring that spedific data, when parsing whatever read from the FIFO).

A very simple example of that:
<?

// path & name of your FIFO-file
$someFIFO = "path/to/your/fifo"

// some string, that won't be found in your regular input data
$uniqueData = "some specific data";

// this statement actually does the trick providing some data waiting in the FIFO:
//   start echo to send data to the FIFO in the background!!
//   NOTE: parenthesis & second redirection (to /dev/null) are
//   important to keep PHP from waiting for echo to terminate! 
system("(echo -n '$uniqueData' >$someFIFO) >/dev/null &");

// now you can safely open the FIFO, without the PHP-process being blocked
$handle = fopen($someFIFO, 'r');

// loop reading data from the FIFO
while (TRUE) {
   
$data = fread($handle, 8192);

   
// eliminate the initially sent data from our read input
    //   NOTE: this is done only in a very simplyfied way in this example,
    //   that will break if that data-string might also be part of your regular input!!
   
if (!(strpos($inp, $uniqueData) === FALSE))    $data = str_replace($uniqueData, '', $data);

// here comes your processing of the read data...
}

?>
info at b1g dot de
24-Oct-2005 05:54
Simple class to fetch a HTTP URL. Supports "Location:"-redirections. Useful for servers with allow_url_fopen=false. Works with SSL-secured hosts.

<?php
#usage:
$r = new HTTPRequest('http://www.php.net');
echo
$r->DownloadToString();

class
HTTPRequest
{
    var
$_fp;        // HTTP socket
   
var $_url;        // full URL
   
var $_host;        // HTTP host
   
var $_protocol;    // protocol (HTTP/HTTPS)
   
var $_uri;        // request URI
   
var $_port;        // port
   
    // scan url
   
function _scan_url()
    {
       
$req = $this->_url;
       
       
$pos = strpos($req, '://');
       
$this->_protocol = strtolower(substr($req, 0, $pos));
       
       
$req = substr($req, $pos+3);
       
$pos = strpos($req, '/');
        if(
$pos === false)
           
$pos = strlen($req);
       
$host = substr($req, 0, $pos);
       
        if(
strpos($host, ':') !== false)
        {
            list(
$this->_host, $this->_port) = explode(':', $host);
        }
        else
        {
           
$this->_host = $host;
           
$this->_port = ($this->_protocol == 'https') ? 443 : 80;
        }
       
       
$this->_uri = substr($req, $pos);
        if(
$this->_uri == '')
           
$this->_uri = '/';
    }
   
   
// constructor
   
function HTTPRequest($url)
    {
       
$this->_url = $url;
       
$this->_scan_url();
    }
   
   
// download URL to string
   
function DownloadToString()
    {
       
$crlf = "\r\n";
       
       
// generate request
       
$req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
           
.    'Host: ' . $this->_host . $crlf
           
.    $crlf;
       
       
// fetch
       
$this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
       
fwrite($this->_fp, $req);
        while(
is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
           
$response .= fread($this->_fp, 1024);
       
fclose($this->_fp);
       
       
// split header and body
       
$pos = strpos($response, $crlf . $crlf);
        if(
$pos === false)
            return(
$response);
       
$header = substr($response, 0, $pos);
       
$body = substr($response, $pos + 2 * strlen($crlf));
       
       
// parse headers
       
$headers = array();
       
$lines = explode($crlf, $header);
        foreach(
$lines as $line)
            if((
$pos = strpos($line, ':')) !== false)
               
$headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
       
       
// redirection?
       
if(isset($headers['location']))
        {
           
$http = new HTTPRequest($headers['location']);
            return(
$http->DownloadToString($http));
        }
        else
        {
            return(
$body);
        }
    }
}
?>
admin at sellchain dot com
17-Oct-2005 08:34
TIP: If you are using fopen and fread to read HTTP or FTP or Remote Files, and experiencing some performance issues such as stalling, slowing down and otherwise, then it's time you learned a thing called cURL.

Performance Comparison:

10 per minute for fopen/fread for 100 HTTP files
2000 per minute for cURL for 2000 HTTP files

cURL should be used for opening HTTP and FTP files, it is EXTREMELY reliable, even when it comes to performance.

I noticed when using too many scripts at the same time to download the data from the site I was harvesting from, fopen and fread would go into deadlock. When using cURL i can open 50 windows, running 10 URL's from each window, and getting the best performance possible.

Just a Tip :)
nefertari at nefertari dot be
20-Sep-2005 06:47
Important note:

You have always to use the real path name for a file with the command fopen [for example: fopen($filename, 'w')], never use a symbolic link, it will not work (unable to open $filename).
francis dot fish at gmail dot com