I need to transfer a file from server1 to server2 using html-php. Right now from server1 the download file works fine with following php code, but instead of downloading to client machine, I need this file file to upload to another server, how can I do this.
Note that the file is not in web directory, I have to read the file from no web directory and transfer to server2.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile("/home/user/file.ini");
Method 1: Serve File to Download
Server 1
$file_name = 'usb.zip';
$file = '/usr/download/' . $file_name;
if (file_exists($file))
{
if(false !== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
//Send Chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
Server 2
file_put_contents("usb.zip", fopen("http://xxx/file.php", 'r'));
Method 2: Upload via SFTP
$resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');
$srcFile = fopen("/home/myusername/".$csv_filename, 'r');
$writtenBytes = stream_copy_to_stream($srcFile, $resFile);
fclose($resFile);
fclose($srcFile);
Related
I'm trying to download this file from DropBox, i tried the same code with a file on the same website that running the PHP script and it did work but now when i add a full ULR, it doesn't.
The problem is that the QR-Kodite.rar file is created in my Windows downloads folder but its size is 0 and Chrome says that the download has finished.
This is my code :
if(isset($_GET['App'])){
if(basename($_GET['App']) == $_GET['App']){
$path = 'https://dl.dropboxusercontent.com/s/pdg8bnpmbgvcsmd/QR-Kodite.rar';
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
header('Content-Disposition: attachment; filename=' . $path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
$file = # fopen($path, 'rb');
if($file){
fpassthru($file);
$_SESSION["ITA"] = "No";
exit();
}
}
}
I have scripted a download file on PHP. I use fpassthru but I cannot download any file. It just print binary code on my page. When I tried uploading it on host and it works fine.
Can anyone explain why my PHP file is not working on local?
Note: I am using XAMPP on Mac.
Here is my code:
$filename = $data['PathFile'];
$directory = '../upload/'.$filename;
if (file_exists($directory) && is_readable($directory)) {
$size = filesize($directory);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
$file = # fopen($directory, 'rb');
if ($file) {
fpassthru($file);exit;
}
Please Check your Directory or path. Giving an example of another code for download please try this.
$file = 'Pareshaan.mp4';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Everytime I use the code below to stream an image file to me the file comes back as corrupt. The image file most certainly is not corrupt though and is fine when I open it through my ftp. Am I missing something?
$fileTry = imagecreatefromstring(file_get_contents($file_path));
if($fileTry === false)
{
die('no file');
}
else
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_path));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
}
I'm trying to use PHP to serve up a file download. I can get the file to begin downloading, but it has only ever been able to download about 280MB of a 1GB file. I'm on a shared host so this could be the issue, if it's not an issue with my code:
function downloadFile($file){
if (file_exists($file)) {
if(is_dir($file)){return false;}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.sprintf("%u", filesize($file)));
ob_clean();
$handle = fopen($file, "rb");
$chunksize=(sprintf("%u", (filesize($file))/1024));
set_time_limit(0);
while (!feof($handle)) {
echo fgets($handle, $chunksize);
flush();
}
fclose($handle);
die;
}else{ echo "file not found";}
return;
}
I search web for a function/command in php/javascript/xul which can transfer a file to local directory from a http server, but didn't find any sufficient answers.
Is there any function/command avaliable to do this job, using any of PHP/Javascript/XUL?
Here is the php script for file download:
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
http://php.net/manual/en/book.ftp.php, here is a good example http://www.php.net/manual/en/ftp.examples-basic.php
Try this function http://www.php.net/manual/en/function.ftp-get.php
This cannot be done with javascript, but in PHP you have to send the proper headers and then serve the file like this:
Example: Force download of CSV file
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");