Hello there
have a nice day..
I have PHP script to upload files on servers, I want to copy uploaded files once they uploaded on server to another server using PHP
ex.
upload file F to server A after that copied to server B with same name F.
how to achive this ?

Dani AI

Generated

Short answer: decouple the upload from the transfer. Let the HTTP request finish as soon as the file is stored on server A, record a small “transfer job” (DB row or queue message) and let a separate worker move the file to server B. This makes the user return quickly and ensures the copy continues even if the browser is refreshed.

A minimal, robust workflow

  • Upload handler: save the file to a local upload folder and insert a job record (pending) with filename, local path, desired remote path, hash, timestamps.
  • Worker: a separate process (long-running daemon, systemd service, or scheduled task) pulls pending jobs, marks them in_progress, transfers using an authenticated channel (SFTP/rsync/SCP or an SDK for object storage), verifies checksum, then marks done and optionally removes the local copy.
  • If you cannot run background processes on the host, have server B pull new files from A (poll an API or list) — that is the pull variation outlined.

Example job table and worker skeleton (illustrative)

CREATE TABLE file_jobs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  filename VARCHAR(255),
  local_path VARCHAR(1024),
  remote_path VARCHAR(1024),
  status ENUM('pending','in_progress','done','failed') DEFAULT 'pending',
  attempts INT DEFAULT 0,
  sha256 CHAR(64),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

# worker (pseudo-PHP)
# select pending job -> set status=in_progress -> transfer via SFTP/SCP/library -> verify sha256 -> set done/failed

Operational tips and security

  • Use key-based SSH/SFTP or API credentials scoped to a single upload-only account.
  • Compute a local SHA256 before transfer and verify after transfer to avoid silent corruption.
  • Implement retries with exponential backoff and a max attempts threshold; log failures and surface them in an admin view.
  • Sanitize filenames, store outside webroot, and serve via signed URLs or a proxy script if needed.
  • For large files consider chunked/direct-to-storage uploads so server A never acts as an expensive transit.

If exec is disabled on your host, use a pure-PHP transfer library (no shell calls) or the pull-from-B pattern above. This gives the UX you want (fast uploads) and reliable, resumable background copying.

Recommended Answers

All 4 Replies

am sorry for not being clear :P, I used FTP functions but I need to upload it in background so user don't wait for too long.so if user by mistake refreshed the page, proccess still work in background.
I know there is a way for using "exec" function but I think my server closed this for security reasons.

You can also use cUrl to call your own pages without waiting for it to finish. The only problem is that you have to implement something to detect that a file is being handled.

My choice would be to implement a cron job that processes files at regular intervals.

Member Avatar for Member #120589

A cron job would be my choice, unless you need this copying to be "immediate". You could of course append a simple files.txt list maybe and reference that from the second site. You don't have to have multiple copies of anything then, you just reference all 'uploaded' files to the first site.
Obviously, you could get even more clever and run a script from the second to the first site - like an API - maybe involving the first site DB.

Many ways see I do.

For example:

Site A:

1) upload file(s) to directory
2) append filename to text file (files.txt)

Site B:

1) get list of files from (file_get_contents or cURL)
2) display files with links (href or src attribute) like: '' . $filename

So you're not duplicating files all over the place. That's a v. simple solution. You could of course set up an API to retrieve specific, e.g



But that's for another discussion. Sorry if I went off campus with that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.