Copy File from Remote Server or Url

Copy file from remote server or URL

While copy() will accept a URL as the source argument, it may be having issues a url for the destination.

Have you tried specifying the full filesystem path to the output file? I'm assuming you're not trying to put the new file onto a remote server.

For example:

$file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/img/submitted/yoyo.jpg';

if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}

The above worked nicely for me.

Best way to get a file from remote server and copy to local server using php

Just use copy

$source = "http://www.remotesite.com/video.avi";
$dest = "video.avi";
copy($source, $dest);

PHP - best way to copy remote file to local filesystem

$remote = fopen('http://...', 'r');
$local = fopen('myfile.xml', 'w');

stream_copy_to_stream($remote, $local);

See http://php.net/stream_copy_to_stream. The simpler alternative would actually be to use copy.

If at all possible, you may want to use rsync instead though, which could make this a whole lot more efficient by transferring only the differences between the two files. I'd set up rsync using a cron job instead of doing this in PHP.

How to copy remote server file in document directory in swift

You can use NSURLSessionDownloadTask to download the file:

func downloadFile(url: URL) {
let downloadRequest = URLRequest(url: url)
URLSession.shared.downloadTask(with: downloadRequest) { location, response, error in
// To do check resoponse before saving
guard let tempLocation = location where error == nil else { return }
let documentDirectory = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask).last
do {
let fullURL = try documentDirectory?.appendingPathComponent((response?.suggestedFilename!)!)
try FileManager.default.moveItem(at: tempLocation, to: fullURL!)
print("saved at \(fullURL) ")
} catch NSCocoaError.fileReadNoSuchFileError {
print("No such file")
} catch {
// other errors
print("Error downloading file : \(error)")
}
}.resume()
}

let stringURL = "https://wordpress.org/plugins/about/readme.txt"
downloadImage(url: URL(string: stringURL)!)

Update: SWIFT 2.2

func downloadFile(url: NSURL) {
let downloadRequest = NSURLRequest(URL: url)
NSURLSession.sharedSession().downloadTaskWithRequest(downloadRequest){ (location, response, error) in

guard let tempLocation = location where error == nil else { return }
let documentDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let fullURL = documentDirectory?.URLByAppendingPathComponent((response?.suggestedFilename)!)

do {
try NSFileManager.defaultManager().moveItemAtURL(tempLocation, toURL: fullURL!)
} catch NSCocoaError.FileReadNoSuchFileError {
print("No such file")
} catch {
print("Error downloading file : \(error)")
}

}.resume()
}

let stringURL = "https://wordpress.org/plugins/about/readme.txt"
let url = NSURL.init(string: stringURL)
downloadFile(url!)

copying a file from a remote server with php copy command

If these are your permissions ls -l /tmp

lrwxr-xr-x@ 1 root wheel 11 Jul 20 23:44 /tmp -> private/tmp – ian 9 hours 

That looks like ordinary processes don't have write permission then. The last r-x means that te other users (not root or wheel group) lack the w write right. Usually Apache runs under a separate accounts, which is why PHP also cannot access it.

Copy File to remote windows server using .net core

Finally I am able to solve the issue by implementing the Win32 API called WNetUseConnection solutions that was mentioned on below questions:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials



Related Topics



Leave a reply



Submit