EisFuX: Datei speichern - Fehlermeldung file_get_contents

Beitrag lesen

(Hallo|Hi(ho)|Tag) anacoma,

Ich habe bisher folgenden Code

Code:

//XML-Datei speichern
$fp = fopen($url, 'rb');
if (!$fp) {
die('kann Datenquelle nicht öffnen');}
file_put_contents($target, $fp);

Leider ist die Funktion bei meinem Webspace Anbieter gesperrt. File_put_contents kann ich nicht ausführen.

Gesperrt oder nicht vorhanden?

Gibt es noch eine andere Methode?

Wenn die Funktion fehlt, kann sie als benutzerdefinierte Funktion "nachgebaut" werden. Eine Möglichkeit ist,
file_put_contents() aus dem PEAR-Paket PHP_Compat zu benutzen. Eine andere wäre, die Funktion selbst nachzubauen:

  
///  
if ( !function_exists('file_put_contents') ) {  
///  
  function file_put_contents(  
    $filename = FALSE,  
    $data = FALSE,  
    $flags = FALSE,  
    $context = FALSE // wenn gueltig, dann vom Typ RESOURCE  
  ) {  
  
    if ( empty($filename) ) {  
      user_error(__FUNCTION__ . '() needs at least 1 parameter, 0 given', E_USER_WARNING);  
      return FALSE;  
    }  
  
    if( 'resource' == gettype($data) ) {  
      ob_start();  
      $bytes = @fpassthru($data); // fpassthru hinterlaesst nutzloses File-Handle  
      $data = ob_get_contents();  
      ob_end_clean();  
      if (FALSE === $bytes) {  
        user_error(__FUNCTION__ . '() failed to open input stream', E_USER_WARNING);  
        return FALSE;  
      }  
    }  
  
    if ( is_string($data) ) $data = array($data);  
    elseif ( !is_array($data) ) {  
      user_error(  
          sprintf(  
              __FUNCTION__ . '() expects parameter 2 to be of STRING, ARRAY or RESOURCE type, %s given',  
             gettype($data)  
          ),  
          E_USER_WARNING  
      );  
      return FALSE;  
    }  
  
    $mode = (FILE_APPEND & $flags) ? 'ab' : 'wb';  
  
    $iua = ignore_user_abort(1);  
    if (FALSE === $context) $fh = @fopen( $filename, $mode, ( (FILE_USE_INCLUDE_PATH & $flags) ? TRUE : FALSE ) );  
    else                    $fh = @fopen( $filename, $mode, ( (FILE_USE_INCLUDE_PATH & $flags) ? TRUE : FALSE ), $context );  
  
    if (FALSE === $fh) {  
      user_error( sprintf( __FUNCTION__ . '() failed to open output stream for "%s": Permission denied', $filename), E_USER_WARNING);  
      return FALSE;  
    }  
  
    $fl = flock($fh, LOCK_NB | LOCK_EX);  
    if (FALSE === $fl) {  
      user_error( sprintf( __FUNCTION__ . '() failed to lock output file "%s"', $filename), E_USER_WARNING );  
      return FALSE;  
    }  
  
    $length = 0;  
    foreach ($data as $val) {  
      $rc = @fputs($fh, $val);  
      if (FALSE === $rc) break;  
      $length += strlen($val);  
    }  
    @fclose($fh); /// fclose also removes locking  
    ignore_user_abort($iua);  
  
    if(FALSE === $rc) {  
      $data_length = 0;  
      foreach ($data as $val) $data_length += strlen($val);  
      user_error(  
          sprintf(  
              __FUNCTION__ . '() only %d of %d bytes written, possibly out of free disk space.',  
             $length,  
             $data_length  
          ),  
          E_USER_WARNING  
      );  
      return FALSE;  
    }  
  
    return $length;  
  }  
///  
}  
///  

Dieser Nachbau deckt die meisten Anwendungsbereiche von file_put_contents() ab und müsste für deine Zwecke ausreichen.

Oder kennt ihr einen nicht so teuren Anbieter, der den Befehl unterstützt?

Sollte es tatsächlich daran liegen, dass bei deinem Hoster noch PHP4 läuft, wäre über einen Wechsel zumindest nachzudenken:

http://www.webhostlist.de/webhosting/search/

MffG
EisFuX