Skip to content
Advertisement

copy() fail to copy image from url to directory

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:xampphtdocswebfrontend/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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement