- PHP file copy to another server; Access filesystem on other server
- PHP file copy to another server; Access filesystem on other server
- PHP: how do I copy a temp file upload to multiple places?
- Copy large files (over 2 GB) in PHP
- How To Copy Files Around FTP Using PHP
- Transfer Files Server to Server Using Simple PHP
- 1. Using PHP Copy to move files from server to server.
- 2. Using PHP FTP to move files from server to server
- ZIP and UNZIP Files using PHP
- ZIP Files using PHP
- UNZIP Files using PHP
- Other Alternative to ZIP / UNZIP File
PHP file copy to another server; Access filesystem on other server
In other words, this can do the job: http://php.net/manual/en/function.copy.php Solution 2: From the manual page on on PHP.net: Solution 3: Unless you’re actually moving files between servers or to somewhere that PHP doesn’t have access, use copy()(php) Solution 1: Perhaps a little known fact: the copy() function in PHP can be used to copy files to an FTP server, though without as much control as you get by using the ftp-specific functions.
PHP file copy to another server; Access filesystem on other server
I’m trying to write a PHP script to copy the files from your local machine to a server:
$destination_directory = ‘I:\path\to\file\’ . $theme_number;
I check the access to the destination folder with that process, and I keep getting the error return. Anyone know what I’m doing wrong? I pretty much have everything else in place. I just don’t know how to access this other server.
Addendum: I accepted an answer below, because it is technically correct, and I was able to get the Apache server to be accepted by the IIS server, however, for what I was trying to accomplish (giving anyone who used the script unfettered ability to move files to the server), it was infeasible. I would’ve had to set up specific functionality on each of their computers. It seems the best workaround would be to establish the script on the server to which you would like to copy your files, and then move them from your local drive to that location in a more traditional means. That would mean a file server with CGI-exec capabilities, though, which our server did not possess.
I’d guess that you are on windows and that you have I: mapped to a share such as \\server2\files .
If so, that’s your problem. These mappings are only avaialble to the current users (eg, the admin account), not to the IUSR account that your php is probably running as (assuming IIS). Solution, don’t use mappings, instead use the full ‘unc’ path name, ie ‘\\server\share\folder\file.ext’, also remember that the IUSR account will need access to these shares/folders/files
Is this other server accessible via I:\path\to\file\\ ?
If PHP is reporting an error opening the directory, you might want to make sure it exists and you have access permissions to it.
Also, the two slashes ( \\ ) may be causing problems too. Try checking that.
$destination_directory = 'I:/path/to/file/' . $theme_number;
You might also want to look at the FTP functions.
How to copy a file from one directory to another using, The copy () function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the …
PHP: how do I copy a temp file upload to multiple places?
how can I copy two times the same file? I’m trying to do something like this:
copy($file['tmp_name'], $folder."1.jpg"); copy($file['tmp_name'], $folder."2.jpg"); copy($file['tmp_name'], $folder."3.jpg");
And how many time does temp files has before it’s destroyed by the server?
I try using move_uploaded_file also, but I can’t make it work. I want to generate 2 thumbs from an uploaded file.
move_uploaded_file will move the file, and not copy it — which means it’ll work only once.
If you are using copy , there shouldn’t be any limit at all on the number of times you can copy : the temporay file created by the upload will only be destroyed at the end of the execution of your script (unless you move/delete it before, of course)
Still, maybe a solution would be to use move_uploaded_file first, and, then, copy ?
A bit like that, I suppose :
if (move_uploaded_file($file['tmp_name'], $folder . '1.jpg'))
This would allow you to get the checks provided by move_uploaded_file .
If this doesn’t work, then, make sure that :
- $folder contains what you want — including the final /
- That $file[‘tmp_name’] also contains what you want (I’m guessing this is some kind of copy of $_FILES — make sure the copy of $_FILES to $file is done properly)
Why doesn’t move_uploaded_file() work? Are you trying to use it twice? You can’t do that, it moves it, so the second time will fail.
I would just use move_uploaded_file() once, and then make the second copy from the location you just moved it to:
move_uploaded_file($uploaded, $destination); copy($destination, $destination2);
I don’t have a reply to your question directly but how about this workaround ?
copy($file['tmp_name'], $folder."1.jpg"); copy($folder."1.jpg" , $folder."2.jpg"); copy($folder."1.jpg" , $folder."3.jpg");
Thanks man, you give me the light.
I made something like this:
$objUpload = new Upload(); $filename = $objUpload->uploadFile($newFile,$folder); // returns a string $objUpload->makeThumb($filename,$folder,"thumbs",139); // makes a 139px thumbnail from the original file uploaded on the first step $objUpload->makeThumb($filename,$folder,"mini",75); // makes another thumb from the same file
Using move_ulploaded_file and copy we can make only one thumb. 🙂
Copy entire contents of a directory to another using php, Both the DOS copy and Unix cp commands will copy recursively — so the quickest solution is just to shell out and use these. e.g. `cp -r $src $dest`; Otherwise you’ll …
Copy large files (over 2 GB) in PHP
I need to copy some big file (6 GB) via PHP. How can I do that? The Copy() function can’t do it.
I am using PHP 5.3 on Windows 32/64.
function chunked_copy($from, $to) < # 1 meg at a time, you can adjust this. $buffer_size = 1048576; $ret = 0; $fin = fopen($from, "rb"); $fout = fopen($to, "w"); while(!feof($fin)) < $ret += fwrite($fout, fread($fin, $buffer_size)); >fclose($fin); fclose($fout); return $ret; # return number of bytes written >
Recent versions of PHP copy files with chunks so today you could use php copy() function safely
If copy doesnt work, you can try with
stream_copy_to_stream( fopen('/path/to/input/file.txt', 'r'), fopen('/path/to/output/file.txt', 'w+') );
Also see https://bugs.php.net/bug.php?id=81145
You could use exec() if it’s a linux machine.
$srcFile = escapeshellarg($pathToSrcFile); $trgFile = escapeshellarg($pathToTrgFile); exec("cp $srcFile $trgFile");
Php — copy, file_get_contents, file_put_contents doesn’t, But the file never completes, it cuts off towards the end of the file. When I download the file directly from the url it’s complete everytime. I’m looking …
How To Copy Files Around FTP Using PHP
I am tring to copy a file from one folder to another using the php ftp functions.
Copy This File: httpdocs/user_images/Services/File 1.jpg
To: httpdocs/user_images/folder11
i have tried to use ftp_fput but i am not have any luck with it.
Perhaps a little known fact: the copy() function in PHP can be used to copy files to an FTP server, though without as much control as you get by using the ftp-specific functions.
In other words, this can do the job:
if(copy('local/file.img', 'ftp://user:password@ftp.example.com/remote/dir/file.img'))
From the manual page on ftp_put on PHP.net:
Unless you’re actually moving files between servers or to somewhere that PHP doesn’t have access, use copy()(php)
file copy: http://us.php.net/manual/en/function.copy.php
PHP File Upload, With PHP, it is easy to upload files to the server. However, with ease comes danger, so always be careful when allowing file uploads! Configure The «php.ini» File …
Transfer Files Server to Server Using Simple PHP
Sometimes you need to move/migrate files to another server/hosting, and you/your client only have FTP access to the server. And to download these files and re-upload to another server can take a lot of time using FTP client such as Filezilla. FTP do not have zip – unzip functionality, so you need to upload it one by one. And server to server transfer is a lot faster than downloading and uploading the files.
You can use this simple PHP script to move files from one server to another server.
1. Using PHP Copy to move files from server to server.
You can just create a php file in the destination server and load the file once in your browser. For example you add this code in http://destination-url/copy-files.php and in copy-files.php you add this php code:
/** * Transfer Files Server to Server using PHP Copy * @link https://shellcreeper.com/?p=1249 */ /* Source File URL */ $remote_file_url = ‘http://origin-server-url/files.zip’; /* New file name and path for this file */ $local_file = ‘files.zip’; /* Copy the file from source url to server */ $copy = copy( $remote_file_url, $local_file ); /* Add notice for success/failure */ if( !$copy ) < echo "Doh! failed to copy $file. \n"; >else
2. Using PHP FTP to move files from server to server
Sometimes using PHP Copy didn’t work if the files is somehow protected by this method (hotlink protection maybe?). I did experience that if the source is from Hostgator it didn’t work.
But we can use another method. Using FTP (in PHP) to do the transfer using the code:
/** * Transfer (Import) Files Server to Server using PHP FTP * @link https://shellcreeper.com/?p=1249 */ /* Source File Name and Path */ $remote_file = 'files.zip'; /* FTP Account */ $ftp_host = 'your-ftp-host.com'; /* host */ $ftp_user_name = 'ftp-username@your-ftp-host.com'; /* username */ $ftp_user_pass = 'ftppassword'; /* password */ /* New file name and path for this file */ $local_file = 'files.zip'; /* Connect using basic FTP */ $connect_it = ftp_connect( $ftp_host ); /* Login to FTP */ $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass ); /* Download $remote_file and save to $local_file */ if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) < echo "WOOT! Successfully written to $local_file\n"; >else < echo "Doh! There was a problem\n"; >/* Close the connection */ ftp_close( $connect_it );
using FTP you have more flexibility, the code above is using ftp_get to import the files from source server to destination server. But we can also use ftp_put to export the files (send the files) from source server to destination, using this code:
/** * Transfer (Export) Files Server to Server using PHP FTP * @link https://shellcreeper.com/?p=1249 */ /* Remote File Name and Path */ $remote_file = 'files.zip'; /* FTP Account (Remote Server) */ $ftp_host = 'your-ftp-host.com'; /* host */ $ftp_user_name = 'ftp-username@your-ftp-host.com'; /* username */ $ftp_user_pass = 'ftppassword'; /* password */ /* File and path to send to remote FTP server */ $local_file = 'files.zip'; /* Connect using basic FTP */ $connect_it = ftp_connect( $ftp_host ); /* Login to FTP */ $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass ); /* Send $local_file to FTP */ if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) < echo "WOOT! Successfully transfer $local_file\n"; >else < echo "Doh! There was a problem\n"; >/* Close the connection */ ftp_close( $connect_it );
To make it easier to understand:
- ftp_connect is to connect via FTP.
- ftp_login is to login to FTP account after connection established.
- ftp_close is to close the connection after transfer done (log out).
- ftp_get is to import/download/pull file via FTP.
- ftp_put is to export/send/push file via FTP.
After you import/export the file, always delete the PHP file you use to do this task to prevent other people using it.
ZIP and UNZIP Files using PHP
Of course to make the transfer easier we need to zip the files before moving, and unzip it after we move to destination.
ZIP Files using PHP
You can zip all files in the folder using this code:
/** * ZIP All content of current folder * @link https://shellcreeper.com/?p=1249 */ /* ZIP File name and path */ $zip_file = 'files.zip'; /* Exclude Files */ $exclude_files = array(); $exclude_files[] = realpath( $zip_file ); $exclude_files[] = realpath( 'zip.php' ); /* Path of current folder, need empty or null param for current folder */ $root_path = realpath( '' ); /* Initialize archive object */ $zip = new ZipArchive; $zip_open = $zip->open( $zip_file, ZipArchive::CREATE ); /* Create recursive files list */ $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root_path ), RecursiveIteratorIterator::LEAVES_ONLY ); /* For each files, get each path and add it in zip */ if( !empty( $files ) ) < foreach( $files as $name =>$file ) < /* get path of the file */ $file_path = $file->getRealPath(); /* only if it's a file and not directory, and not excluded. */ if( !is_dir( $file_path ) && !in_array( $file_path, $exclude_files ) )< /* get relative path */ $file_relative_path = str_replace( $root_path, '', $file_path ); /* Add file to zip archive */ $zip_addfile = $zip->addFile( $file_path, $file_relative_path ); > > > /* Create ZIP after closing the object. */ $zip_close = $zip->close();
UNZIP Files using PHP
You can unzip file to the same folder using this code:
/** * Unzip File in the same directory. * @link http://stackoverflow.com/questions/8889025/unzip-a-file-with-php */ $file = ‘file.zip’; $path = pathinfo( realpath( $file ), PATHINFO_DIRNAME ); $zip = new ZipArchive; $res = $zip->open($file); if ($res === TRUE) < $zip->extractTo( $path ); $zip->close(); echo «WOOT! $file extracted to $path»; > else
Other Alternative to ZIP / UNZIP File
Actually, if using cPanel you can easily create zip and unzip files using cPanel File Manager.
I hope this tutorial is useful for you who need a simple way to move files from server to server.