0 レビュー
3 回答
php-URLからサーバーに画像をコピーします:そのようなファイルやディレクトリはありません
phpを使用してURLから画像を取得し、サーバーにコピーしていますが、そのようなファイルがないというエラーが発生しました
画像の例:
http://www.google.co.in/intl/en_com/images/srpr/logo1w.png
これが私が使用している解決策です:
//ファイルを取得する
$ content = file_get_contents( "http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//ファイルシステムに保存します。
$ fp = fopen( "/ location / to / save / image.jpg"、 "w");
fwrite($ fp、$ content);
fclose($ fp);
次のエラーが発生します。何か間違ったことをしましたか?
fopen(/location/to/save/image.jpg): failed to open stream: No such file or directory
わからない
0
レビュー
答え :
解決策:
✓これは私にとってはうまくいきました。
なしで試してください
/location/to/save/
ファイルは、スクリプトを実行したのと同じフォルダーに保存されます。
例:
<?php //Get the file $content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png"); //Store in the filesystem. $fp = fopen("image_google.jpg", "w"); fwrite($fp, $content); fclose($fp); ?>
わからない
0
レビュー
答え :
解決策:
function download_image($url,$destination_path = '')
{
// CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else
{
$content = file_get_contents($url);
}
// CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
if(!file_exists($destination_path) && $destination_path != ''){
mkdir($destination_path, 0755, true);
}
// ATTEMPT TO CREATE THE FILE
$fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
fwrite($fp, $content);
fclose($fp);
}
download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。