Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kaustubh's avatar

Copy Files From URL to Server Public Folder

Hi I have two project in which one project contains files and another one dont have file. I want to copy the file from one server to another server.

Server 1 URL = http://localhost/jobseeker/public/datafiles/APPL/JOBSEEKER_trOoH.pdf

Server 2 URL = http://localhost/recruiter/public/

I want to copy server1 file in server2 public folder

    $file1 = REQUEST('cand_resume_url');
    $file2 = public_path('datafiles\APPL');
    File::move($file1, $file2);

I used Copy, Move but it throwing error

The file "http://localhost/mwayhire_jobseeker/public/datafiles/APPL/JOBSEEKER_trOoH.pdf" does not exist

0 likes
3 replies
lostdreamer_nl's avatar
Level 53

By 'copying' in this case you actually mean 'downloading': You need to download some file of another server via http, and save it to a local folder.

Depending on how your server is setup, and which types of files you want to download the easiest way is basically :

  • checking the URL for a filename with extension
  • making sure the extension is allowed to be downloaded
  • using file_get_contents($url); to download the file into a variable
  • saving the file to your path with the same name / extension.

In plain php this would be something like:

$allowedExtensions = ['csv', 'doc', 'pdf', 'jpg'];
$path = public_path('datafiles\APPL');

$url = request('cand_resume_url');
$fileName basename($url);
$extension = pathinfo($filename, PATHINFO_EXTENSION);

if(array_search($extension, $allowedExtensions) === false) {
    throw new \Exception($extension .' is not allowed');
}
if(file_exists($path . $fileName)) {
    throw new \Exception('File exists');
}

file_put_contents($path .'/'. $fileName, file_get_contents($url));
1 like
Kaustubh's avatar

thanks @lostdreamer_nl

i got the solution

  file_put_contents(public_path('datafiles\APPL').'/'.$resume, fopen(REQUEST('cand_resume_url'), 'r'));
lostdreamer_nl's avatar

@Kaustubh Just make sure to also check (at the very least) for the file's extension, you wouldn't want to have a user download their php script to your server.

Please or to participate in this conversation.