473,461 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cURL Issue: prompt user to save file

384 256MB
I've have the below code and its works fine, how can I get the file to prompt the user to save it instead of specifying a path on the server to save it?

I have to use curl due to hosting and memory issues on the hosting.

Any help much appreciated.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $contentType = 'text/plain';
  3. $file = 'http://mysite.com/test.txt';
  4.  
  5. $fp = fopen('/path/to/save/mytest.txt', 'w');
  6. $ch = curl_init($file);
  7. curl_setopt($ch, CURLOPT_FILE, $fp);       
  8. $data = curl_exec($ch);       
  9. curl_close($ch);     
  10. fclose($fp);
  11. ?>
Jul 15 '11 #1

✓ answered by johny10151981

readfile function do not load the file into localmemry, it directly load to output buffer. if it is disable you better create your own function that will behave like readfile function.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $handle = fopen($path);
  3. while (!feof($handle)) {
  4.   echo fread($handle, 1);
  5. }
  6. fclose($handle);
  7. ?>

9 4525
johny10151981
1,059 1GB
Its not impossible, but not easy,

What you can do:
you can map the directories and create a directory tree.

so that user can browse and choose a place to save.
and I am positive that this will be your open hole to crack and burn your server.
...................
Jul 16 '11 #2
ziycon
384 256MB
Ok, I'm thinking this option wont be the best so here my dilema, I need a user to be able to click a link to download a file but I need to capture that link click to update the database log table to track what registered users download, any ideas to the best wasy to capture this link click?

I can't store the files in the database and can't use a script to build the file into a downloadable file due to the size of some of the files 100mb - 200mb+.

Thanks for the help.
Jul 18 '11 #3
johny10151981
1,059 1GB
in that case never provide the direct link of the file. moreover block the directory from accessing file. Then create a simple download file. everytime you would create a link, you will have to create it with javascript, example below:
Expand|Select|Wrap|Line Numbers
  1. <a href='javascript:document.location="download.php?fileName=desire.pdf"'>desire</a>
  2.  
download.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $fileName=$_GET['fileName'];
  3. //do some security check and other operation. before i have posted a script. find it out. it will help a lot
  4. readfile($fileName);
  5. ?>
  6.  
Jul 18 '11 #4
ziycon
384 256MB
Thanks for your reply johny10151981, the reason I was going for capturing the click was due to readfile() being disabled on my hosting for security reasons and the size of some of the files are 200ish MB so I can't really use a function that reads the file into memory first as the server would most definitly fall over.
Jul 18 '11 #5
johny10151981
1,059 1GB
readfile function do not load the file into localmemry, it directly load to output buffer. if it is disable you better create your own function that will behave like readfile function.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $handle = fopen($path);
  3. while (!feof($handle)) {
  4.   echo fread($handle, 1);
  5. }
  6. fclose($handle);
  7. ?>
Jul 18 '11 #6
johny10151981
1,059 1GB
i think this also can be done
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*database issues*/
  3. $fileName=$_GET['fileName'];
  4. header('Location: www.mysite.com/links/'.$fileName);
  5. exit;
  6. ?>
Jul 18 '11 #7
ziycon
384 256MB
This code does the job but what affect will/can it have on the memory usage of the server and the limitations of the memory used per script in php.ini?

For example in php.ini its set to: 'memory_limit = 32M', so I assume this code can't handle a file greater the 32mb!?
Expand|Select|Wrap|Line Numbers
  1. $contentType = 'application/pdf';
  2. $path = 'http://path/to/file/test.pdf';
  3.  
  4. header("Cache-Control: public");
  5. header("Content-Description: File Transfer");
  6. header("Content-Type: $contentType");
  7. header("Content-Transfer-Encoding: binary");
  8. header("Content-Disposition: attachment; filename=".basename($path));
  9.  
  10. $handle = fopen($path,'r');
  11. while (!feof($handle)) {
  12.   echo fread($handle, 1);
  13. }
  14. fclose($handle);
Jul 18 '11 #8
johny10151981
1,059 1GB
Absolutely not.

It mean you cannot allocate memory in RAM more than 32 Mega byte.

When you open a file, it does not load the file into RAM. If it was it would be impossible to handle big database system.

it is possible to limit upload and download file size in your php.ini. in that case you should provide multi-part file download support
Jul 19 '11 #9
ziycon
384 256MB
I have a script in place that will chunck the file being downloaded into 30mb bits to get around the php.ini size restriction, its seems to be working grand for the past few hours.
*Fingers crossed*

Thanks for the help and advice johny10151981.
Jul 20 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Conrad F | last post by:
Hi, If any Microsoft people are listening.... Are there any plans for the new web language called "Curl" to be supported in .NET (ASP.NET)? I ask as Curl represents the first step to true OO...
4
by: yawnmoth | last post by:
Is it possible to send http requests with curl but not have curl wait for the response? The reason I ask is because I'd like to code a web app that can sorta start time consuming processes...
4
by: Terry | last post by:
I'm using curl to invoke a php script on the same site/server. It works great, but if I call it again while it's still running, nothing happens. Why? Can that be fixed? Why use curl? To make...
0
by: apinti | last post by:
Here is my code, and the problem: $st="...(string to post is formed from different variables read from file)..."; $lngth=strlen($st); $ch = curl_init(); curl_setopt($ch,...
5
by: apinti | last post by:
Here is my code, and the problem: $st="...(string to post is formed from different variables read from file)..."; $lngth=strlen($st); $ch = curl_init(); curl_setopt($ch,...
1
by: devranger | last post by:
Hello, I am in the process of learning PHP CURL and in doing some test examples I am having some issues with CURL that I do not fully understand. I am doing a test to use CURL to post and return...
3
by: Matthias Leopold | last post by:
hi i've got problems getting php4 scripts which use curl to work on RHEL4 (Nahant Update 4) and CentOS 4.4. when the script is accessed the browser "hangs", after a couple of minutes i'm...
4
by: rottmanj | last post by:
I am working on a revamp of a previous application that I have written in coldfusion. The application deals with RETS data. The issue that I am having is with using the curl option HTTP_AUTH and...
3
by: Sarah | last post by:
I was wondering if someone might be able to help me with this issue. I have a feeling this has something to do with my host's server settings as I used to be able to get CURL to follow redirects by...
5
by: DustinP | last post by:
I have installed PHP5 as ISAPI several times per every single guide I could find in the first 50 pages of results on google.. Server is Windows 2008 runing IIS7, phpinfo works and tells me the...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.