downloading and saving image in blackberry using php and javascript - php

I am working on Blackberry webworks App. I am displaying some images on the page. I want to save the image in the blackberry Picture folder if user click on it. I used the following code, but its saving it in the memory card, not in the blackberry picture folder. Following is my code:
$file = 'images/' . $_GET['file'];
if (file_exists($file)) {
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
I have found another way of saving file in blackberry, but I don't understand how to save the image using the following code, its the javascript code that I have got from blackberry forum.
<script type="text/javascript">
var xmlString = "<test>IO functions</test>";
var filePath = "file:///store/home/user/sample.xml";
var parser = new DOMParser();
var doc = parser.parseFromString(xmlString, "text/xml");
var blob_data = blackberry.utils.documentToBlob(doc);
blackberry.io.file.saveFile(filePath, blob_data);
</script>

I also had this problem and since I couldn't find any solution in the API, I've decided to develop an Javascript Extension for it. You can download it from GitHub https://github.com/dbotelho/blackberry.io.filetransfer

Related

Why is the browser running in circles and does not want to force the download of the PDF file in PHP?

I have created a script that allowed me to download docx, srt and pptx files and many others....
But problem, my script does not want to force the download of PDF files. The Chrome browser is running in circles and still doesn't want to launch the download of my PDF file.
Here is my script:
<?php
require_once 'includes/constants.php';
$pathFileUrl = '../'.WEBSITE_NAME_URL_FILES.'/docs/'.urldecode($fileUrl);
if (isset($fileUrl) && !empty($fileUrl) && file_exists($pathFileUrl)) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $pathFileUrl);
finfo_close($finfo);
header('Content-Description: File Transfer');
header('Content-Type: '.$mime); //application/octet-stream
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($pathFileUrl).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pathFileUrl));
// Read file and send to browser
readfile($pathFileUrl);
exit;
} else {
redirect(WEBSITE_NAME_URL);
}
Can you please tell me where the error is coming from?
Any help would be appreciated :)

PHP-Download doesn't work properly

I want to dowload as a web-client a photo from my webserver with PHP.
Therefore, I've written the following PHP-Script:
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile("/var/www/_old/pictures/".$filename);
($filename is the name of the file, like screenshot_03.06.2016-19:36:50.jpg)
The problem is, the content of this downloaded image is not the image itself, it's the code from the current website. The download works fine, but I cannot open the image properly ...
OK.
After some time, I found a working solution.
My problem was, that I wrote the header-commands AFTER some output. According to the php documentation, this is forbidden.
So all I did is this, I created a new .php-file and checked if the session (in which I previously stored the needed information - filename and download-command) was set correct. On the previous page, I received the POST-information, stored it into the session and redirected (also with header-command) to the newly created php-file.
Then I wrote these header-commands:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);
And now, the download works from all browsers (also IE!).
Here is all my code, I hope this could help someone:
1st page (where I receive the POST-infos):
session_start();
...
...
if(isset($_POST["galerie_submitBt"])){
if($_POST["galerie_submitBt"] === "Herunterladen") { //Das Herunterladen muss in einer eigenen Datei passieren, bevor jeglichen Outputs
$_SESSION["download_Screenshot"] = true;
$_SESSION["filename"] = "/var/www/_old/pictures/".$_POST["filename"];
header("Location:modules/php/download_Screenshot.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>...
2nd, new created php-page (where I actually download the image):
session_start();
if(isset($_SESSION["download_Screenshot"])){
if(isset($_SESSION["filename"]) && file_exists($_SESSION["filename"])) {
$filename = $_SESSION["filename"]; //Filename also includes the path! (absolute path)
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);
unset($_SESSION["filename"]);
unset($_SESSION["download_Screenshot"]);
}
}

PHP Coding for downloading the image

In the website page contains many images with downloading options. If I click the download button it automatically downloaded on user system and it shows on browser downloadable page. I have PHP code like
$image = file_get_contents('http://website.com/images/logo.png');
file_put_contents('C:/Users/ASUS/Downloads/image.jpg', $image);
Above coding is working fine. But I need to provide the path name for image to save. In user side we don`t know the path.
I need the PHP code to use the browser download location and download images need to show the browser downloads.
not possible to store the image in particular user location due to security issues .you don't force user .you have to suggest him to store particular location .and also you don't know the what file system there in user system.and also downloading path can be setting up by user anywhere so your not able to get that.
$filename = '/images/'.basename($_POST['text']);
file_put_contents($filename, $content);
you have to save/download the image somewhere on your web serwer and next send the file to user using header function, for example:
$file = 'path_to_image';
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;
}
else {
echo "file not exists";
}
manual
`<?php
$filename ='http://website.com/images/logo.png';
$size = #getimagesize($filename);
$fp = #fopen($filename, "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
}
header("HTTP/1.0 404 Not Found");
?>`

Download file once - Corrupt file error

What I want to do is to allow only one download per user (one access to a href) For that I have a variable in the user's table, which I do change when the link has been clicked.
I use "download.php?file=file.xxx" for doing that.
download.php
$file= basename($_GET['file']);
$root = "documents/rece/";
$path= $root.$file;
echo $path;
if (is_file($path))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path);
}
else
echo "File error";
exit();
?>
I also update the DDBB and that works. After that I can show or hide the link. The problem is that the downloaded file is corrupted and can't be opened. I will use it with pdf or doc, maybe zip.
Could it be because of the path?
PDF files, as far as I know, start with something like this:
%PDF-1.4
Yours start with 4 blank lines plus documents/rece/Form.pdf%PDF-1.4. You're obviously printing it yourself somewhere before the code you've posted.

PHP multiple file download

I've seen this example on the documentation for PHP readfile
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
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));
ob_clean();
flush();
readfile($file);
exit;
}
?>
How can you make it so It download multiple files say monkey.gif and girraffe.jpg
Preferably without ZIP files...
You can't. It's not a PHP limitation, it's an HTTP/Web-Browser limitation. HTTP doesn't provide a mechanism for sending multiple files over one request.
You could, however, have some PHP script that generates multiple iframes, which would initiate one download each, and fake it that way.
the whole method seems a bit pointless as a physical file actually exists on the server. just use JavaScript to open all the file urls, if you have set the header correctly in your .htaccess file then the files will just download.
I would do something like this
<script>
var files = ['filename1.jpg', 'filename2.jpg'];
for (var i = files.length - 1; i >= 0; i--) {
var a = document.createElement("a");
a.target = "_blank";
a.download = "download";
a.href = 'http://www.example.com/path_to/images/' + files[i];
a.click();
};
</script>

Categories