0

カールがインストールされていません。このコードが機能するはずであることは前に述べましたが、

file_put_contents($target_path,file_get_contents($image));

しかし、それは私にとってはそうではありません。正しい名前などで画像を配置しますが、サイズは0バイトです。$target_path には適切な権限があり、allow_url_fopen はオンになっています。

私は何か間違ったことをしていますか?

4

3 に答える 3

2

allow_url_fopenfile_get_contentsURL が壊れる原因となる唯一の基準ではありません。サーバーは、認証/ユーザーエージェントなどを処理または検出するように設定されている場合があります。

まず、データを変数に取得して出力し、コンテンツを取得できるかどうかを確認してください。

$img = "";
$img = file_get_contents($image);
echo $img; //for debugging..
//for running..
if(!$img) die("no data fetched");

データがある場合$imgは、次にファイルへの書き込みを試みます。

$result = file_put_contents($target_path,$img);
if($result=== FALSE) 
  die("Error writing data into $target_path");
else
  echo "$result bytes written to $target_path";

あなたが直面している問題は、ネストされた関数リストに複数の障害点があることです。また、複数行のコードを 1 行にコンパクトにまとめることができますが、デバッグが困難であり、直面しているような場合には、どれが問題のあるコードであるかを簡単に判断することはできません。

于 2013-04-29T19:39:53.550 に答える
0

コードを分割して、いくつかの基本的なエラー処理を追加してみてください。これにより、エラーが発生した場合にファイルに 0 バイトを書き込むだけでなく、おそらく何かを行うことができます。

<?php 
error_reporting(E_ALL);
$target_path = "./some_path/";
$new_img = "new_image.jpg";

if(file_exists($target_path)){
    $img = file_get_contents($image);
    if(strlen($img) >=1){
        file_put_contents($target_path . $new_img, $img);
    }else{
        echo 'Error fetching $image';
    }
}else{
    echo '$target_path not found';
}
?>
于 2013-04-29T19:41:40.740 に答える
0

パスとURLが正しいことを確認$imageし、コンテンツを機能させる必要があります。正しいターゲットパスを持つのに役立つなど 、使用するのに適しています$target_pathdirname(__FILE__)$target_pathdirname(__FILE__).'/image.jpg'

フルパスを使用する必要があることに注意してください $target_path

fopen で動作するこの関数をテストすることもできますが、動作は遅くなります

  function Request($url,$File="",$Method='POST',$data=null, $optional_headers = null,$Debug=0){
            $params = array('http' => array('method' => $Method));
            $optional_headers.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
            $optional_headers.="Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n";
            $optional_headers.="Accept-Encoding:gzip,deflate,sdch\r\n";
            $optional_headers.="Accept-Language:en-US,en;q=0.8\r\n";
            $optional_headers.="Cache-Control:max-age=0\r\n";
            $optional_headers.="Connection:keep-alive\r\n";
            $optional_headers.="User-Agent:Mozilla/5.0  AppleWebKit/536.5 (KHTML, like Gecko) SepidarBrowser/1.0.100.52 Safari/536.5\r\n";
            if ($data !== null) {
                   $params['http']['content'] = $data;
            }                              
            if ($optional_headers !== null) {
                   $params['http']['header'] = $optional_headers;
            }
            $ctx = stream_context_create($params);
            $fp = @fopen($url, 'rb', false, $ctx); 
            if (!$fp) {
                    return false;                     
            }
            $response= @stream_get_meta_data($fp);
            $out['header'] = $response['wrapper_data'];
            $out['body']='';
            if($File!=""){
                $fout = @fopen($File, 'w+');
            }
            while(!@feof($fp)){
                  $buffering=@fread($fp,1024*8);
                 // echo "***************\n".strlen($buffering)."\n".$buffering."\n***********************";
                  if($buffering==''){break;}
                  if($File!=""){
                      @fwrite($fout,$buffering);
                      if($Debug==1)echo strlen($buffering)."-->Download And Stored IN".$File;
                  }
                  $out['body'] .=$buffering;
            } 
            if(trim(@$out['header']['Content-Encoding'])=='deflate'){
                $out['body']=gzinflate($out['body']);
            }
            fclose($fp);
            return $out;
}
Request($target_path,$image,'GET');
于 2013-04-29T19:39:11.230 に答える