Anyone face this problem before?
like i implement a backup php script.
when i click the button download.
the file will auto download and store in my localhost folder.
but when i upload the script to server and try to run, the script can run and display the successful message. but no any file download.
//SAVE THE BACKUP AS SQL FILE
$handle = fopen($DbName.'-Database-Backup-'.$table.date('Y-m-d #h-i-s').'.sql','w+');
fwrite($handle,$data);
fclose($handle);
below is the script that i wrote.
Php script
Try using the following header in php.
Example:
header('Content-disposition: attachment; filename=my.sql');
header('Content-type: text/plain');
Related
I am trying to download a file using php script. The file is in a database. I extracted the file content and I am printing it out with a header to download.
It works fine on my localhost but when code is running in the cloud, it then displays the file in the browser instead of downloading it.
I did search all similar cases, and I am doing what the best solutions recommend.
Here is my code:
$fcontent = $row->content;
header("Content-Type:application/octet-stream");
header("Pragma:public");
header("Content-Description:File Transfer");
header("Content-Transfer-Encoding:Binary");
header("Content-Disposition:attachment;filename=\"$fname\"");
header("Content-Length:" . $fsize);
header("Expires:0");
header("Cache-Control:must-revalidate");
ob_clean();
flush();
print ($fcontent);
die();
Hello need to download generated csv file and i'm used below code but on server i'm not able to download csv file. The same code working fine in my local sytem but not working on live server.
<?php
function downloadFile($filename,$filepath)
{
header("Content-type: text/csv"); // instead of pdf use others... for text
header("Content-disposition: attachment; filename=".$filename);
readfile($filepath);
}
?>
check your server, may be there is space limitation.
I have succesfully installed youtube-dl on my server and from command line everything is working like a charm.
Now I want to be able to call a link on my site from my web browser which directly initiates a download of the file. So in that order:
Open site in browser (for example http://example.com/download.php?v=as43asx3
YouTube-dl processes the input
Web-browser downloads file
Temporary files will be deleted from server
I am not very experienced with this, but nevertheless need to solve this issue.
There might be a better way of doing this and I would appreciate seeing it, however, here is how I solved it:
<?php
ignore_user_abort(true);
//getting ID from URL
$youtubeID = $_GET['videoID'] ;
//getting audio file from youtube video via youtube-dl
exec('~/bin/youtube-dl --verbose --extract-audio -o "~/html/YouTubeDownloader/%(id)s.%(ext)s" '.$youtubeID);
//downloading file to client/browser
$filename = $youtubeID.".m4a";
header("Content-disposition: attachment;filename=$filename");
header("Content-type: audio/m4a");
readfile($filename);
//deleting file again
unlink($filename);?>
I am using the following code which I referred from the php.net example:
<?php
// open the file in a binary mode
$name = './db.sqlite';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($name));
// dump the database and stop the script
fpassthru($fp);
exit;
?>
When I try to run this PHP script from the URL of my browser, I get the warning:
Cannot modify header information - headers already sent by
My requirement is to basically set up a PHP script which I can use to download the .sqlite file in my iOS app. But I think first it must work directly through the URL. I am new to PHP so please guide me.
I also tried to find out why am I getting this warning in the following link:
http://www.tech-recipes.com/rx/1489/solve-php-e
But it didn't solve my problem.
Question 2.) I have planned to use NSURLSession to get the file in my iOS app's sandbox, once the PHP script is up and working. Is this the correct way?
I could not get my PHP working correctly so I found a workaround.
I used NSURLRequest to point directly to the sqlite file residing on the server and then used NSData to store the contents of it. I then stored the file in the sandbox of my app(simulator) and I am able to query the database.
Important lesson that I learnt: "SQLite database files are not human-readable"
I want to allow users to download large video files. These files are outside of the public folder because of security reasons.
I'm using a combination of fopen(), feof(), and fread() to download the file in chuncks.
The download works fine. The video is downloaded and also works just fine. The problem is during the download. Any user who's downloading the file can't continue browsing the site until the file is downloaded. The browser is trying to establish a connection, but it hangs while the file is downloading. When the download is done, the connection is immediately established. Other users can browse the site just fine during the download, so it's not like the whole server hangs or whatever.
I'm working with PHP (CakePHP) installed on an IIS server.
A snippet of code:
$name = "filename.mp4";
$folder = "private/folder/";
$handle = fopen($folder.$name, "rb");
if(!$handle)
{
echo "File not found";
}
else
{
header("Content-length:".filesize($folder.$name));
header("Content-Type: video/mp4");
header("Content-Disposition: attachment; filename='filename.mp4'");
header("Content-Transfer-Encoding: binary");
session_write_close(); // this is the solution
while(!feof($handle))
{
$buffer = fread($handle, 1*(1024*1024));
echo $buffer;
ob_flush();
flush();
}
}
I finally solved the problem. As suggested above, the problem was indeed related to sessions. Even though session.auto_start was off, CakePHP itself was handling sessions at the moment. So, by inserting session_write_close() right before the while loop, the problem was solved.