0

I'm trying to copy an image from a URL to my server, but it doesn't save. Below is my code

    $year ='2018';
    $make = 'jeep';
    $model = 'grand-cherokee';
    $url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
    $tmp_filename = explode('/images/auctions/', $url);
    
    $filename = $tmp_filename[1].'.jpg';
        
    //server path
    $path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
        
    $_path = pathinfo($path);
    if (!file_exists($_path['dirname'])) {
       mkdir($_path['dirname'], 0755, true);
    }  
    
    //copy image to server
    if (!is_file($path)) {
       copy($url, $path);
       //move_uploaded_file($url, $path);
    } 

The image URL in $url, doesn't have a file extension, but the image is a .jpeg

i tried both copy() and move_uploaded_file() but failed.

when i use copy() i get this error

copy(c:\xampp\htdocs\web\frontend/www/storage/auction/us/2018/jeep/grand-cherokee/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832.jpg): failed to open stream: No such file or directory

I also tried with directory 2018/jeep/grand-cherokee exist and without too. what am is wrong here? Thanks

Shaho
  • 157
  • 3
  • 15
  • What is `$photoURL`? `move_uploaded_file()` is for files that have been _uploaded_ to your server – brombeer Nov 13 '20 at 16:28
  • @kerbh0lz typo in my question. fixed. sorry about that – Shaho Nov 13 '20 at 17:48
  • Does this path exist on your server? `c:\xampp\htdocs\web\frontend/www/storage/auction/us/2018/jeep/grand-cherokee/` – kmoser Nov 13 '20 at 19:51
  • @kmoser yes, and when i delete the path i still get the same error – Shaho Nov 13 '20 at 20:32
  • Does this answer your question? [PHP - Failed to open stream : No such file or directory](https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – kmoser Nov 13 '20 at 21:50

1 Answers1

0

found the problem, Guess the filename was too long? I got this error when i tried manually saving the image from browser to desktop

a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832
The File name. directory name or volume label syntax is incorrect

I renamed the file here

 $url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
   // $tmp_filename = explode('/images/auctions/', $url);
    
    //$filename = $tmp_filename[1].'.jpg';
    $filename = $year.'-'.$make.'-'.$model.'.jpg';
        
    //server path
    $path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
        

and it works now.

Shaho
  • 157
  • 3
  • 15