473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

call to method

Hi,
this code perfectly works when I try to force the download of a file,
by calling it directly.

<?php

$file = "onefilename.txt";
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//
$filename = basename($file);
//
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//
$ctype="application/force-download";
header("Content-Type: $ctype");
//
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;

?>

Now, I can't get this working as a method of a class:

function downloadFile($theFile){

$file = $theFile;
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//
$filename = basename($file);
//
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//
$ctype="application/force-download";
header("Content-Type: $ctype");
//
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}
After instantiating the class, I call
instance.downloadFile("myFile.txt") but I get no prompt.

What am I doing the wrong way?

Thanks.

Nov 28 '05 #1
7 1398
Use $instance->downloadFile(). The dot (.) is used for concatenating
strings. The arrow (->) to call methods in classes.

Nov 28 '05 #2
After instantiating the class, I call
instance.downloadFile("myFile.txt") but I get no prompt. What am I doing the wrong way?


Erm, how about:

instance->downloadFile("myFile.txt");

and turn error reporting on so PHP tells you all about your coding
errors.

---
Steve

Nov 28 '05 #3

That should be:

$instance->downloadFile("myFile.txt");
and turn error reporting on so PHP tells you all about your coding
errors.


and I should practice what I preach 8-)

---
Steve

Nov 28 '05 #4
I beg your pardon:
I wrote in JS manner what I did the way you suggest.

I really don't understand why it doesn't work...

Nov 28 '05 #5
Hi again,
now it "perfectly" works...
even if there's another perfectly strange thing:
I get the prompt, I get the file downloaded BUT the file appears
corrupted and with more bytes than the original (original: 5.75Kb,
downloadedFile: 6.18Kb). Anybody knows what's happening today? :-)

Cheers.

Duff B ha scritto:
I beg your pardon:
I wrote in JS manner what I did the way you suggest.

I really don't understand why it doesn't work...


Nov 28 '05 #6
I get the prompt, I get the file downloaded BUT the file appears
corrupted and with more bytes than the original (original: 5.75Kb,
downloadedFile: 6.18Kb). Anybody knows what's happening today? :-)


A couple of questions:

Where does variable $len come from? It looks like it must be a global,
since it isn't assigned in your method, so how is it asssigned?

Are you sure that binary is the correct format for a text file? Could
there be some unintended translations of CR/LF characters going on as a
result?

---
Steve

Nov 28 '05 #7
Hi Steve, and thanks:
$len came from a filesize, so by correcting it I got the file properly
downloaded.

Are you sure that binary is the correct format for a text file? Could
there be some unintended translations of CR/LF characters going on as a
result?

---
Steve


Nov 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vijay Kumar | last post by:
hi, I have written a trace function in C using the Python/C API. I want to find whether the call occured is a function call or method call and if a method call, its self object. int...
0
by: Eugene Safrankow | last post by:
Hello All! I've encountered with the error when I call a method of dependency library (written in managed VC++) from Smart Client placed on a web page. In general, I make a call to the Windows...
9
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll;...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
5
by: Stephen Barrett | last post by:
I have read many threads related to async fire and forget type calls, but none have addressed my particular problem. I have a webpage that instantiates a BL object and makes a method call. The...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
2
by: jojoba | last post by:
Hello to all! I have a fairly simple webservice running in asp.net ajax under c# (vs 2008). I built the service and it runs just dandy when i test it by itself in visual studio. However, to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.