2

I have this code:

$url     = 'http://www.bgelectronics.eu/image/cache/data/cantell kabeli /14222-228x228.jpg';
$headers = get_headers($url, true);
var_dump($headers);

this returns error for missing file because of the whitespace in the file name:

array(6) { [0]=> string(34) "HTTP/1.1 500 Internal Server Error" ["Date"]=>     
string(29) "Tue, 24 Mar 2015 16:11:18 GMT" ["Server"]=> string(6) "Apache" 
["Content-Length"]=> string(3) "677" ["Connection"]=> string(5) "close" 
["Content-Type"]=> string(29) "text/html; charset=iso-8859-1" } file size:677

Any suggestions please?

Europeuser
  • 934
  • 1
  • 9
  • 32
  • Your spaces probably need urlencoding – Mark Baker Mar 24 '15 at 15:20
  • this sounds idea ! let me test – Europeuser Mar 24 '15 at 15:22
  • 2
    Then perhaps you need to consider the note in the [PHP docs](http://www.php.net/manual/en/function.file-exists.php) "`Tip - As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality`", and note that [http://](http://www.php.net/manual/en/wrappers.http.php) does not support `stat()` – Mark Baker Mar 24 '15 at 15:27
  • `servImagesDir` should probably be `$servImagesDir` –  Mar 24 '15 at 15:59
  • no man, this is my config images path.. Problem is in the remote image, not on local server.. Test to see if you can copy this image and than open it.. – Europeuser Mar 24 '15 at 16:01

2 Answers2

2

The problem is that file_exists() is for the file system, not http. You need to use server directory path. If it is on the same server as your code, it should rather look like:

if(file_exists('image/cache/data/cantell kabeli /202441-500x500.jpg')){
....

if on remote server, try:

if(file_get_contents('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg')){
...

You can find many other ways here: How can one check to see if a remote file exists using PHP?

Community
  • 1
  • 1
n-dru
  • 9,285
  • 2
  • 29
  • 42
0

Try with this :

copy('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', '/image.jpeg');

if not, use file_get_contents

//Get the file
$content = file_get_contents("http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
Ahmed Ziani
  • 1,206
  • 3
  • 14
  • 26
  • #Ahmed, can you please try to copy that image with your method to see you will succeed to copy but the image can not be opened, can you please try? – Europeuser Mar 24 '15 at 15:30
  • i forget to add this , '/image.jpeg' can you try now ?: copy('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', '/image.jpeg'); – Ahmed Ziani Mar 24 '15 at 15:36
  • same thing, did you try it also? Could you see/open the image? – Europeuser Mar 24 '15 at 15:37
  • try with this and show us the error : copy('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', 'image.jpg'); with "http :// www." – Ahmed Ziani Mar 24 '15 at 15:48
  • Warning: copy(bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg): failed to open stream: No such file or directory in /www/..../...../root/example2.php on line 18 – Europeuser Mar 24 '15 at 15:50
  • But the error is because of the wrong image path.. not related to the fact that image can not be copied successfuly.. – Europeuser Mar 24 '15 at 15:51