Jump to content

fopen external files


rhyspaterson

Recommended Posts

Hey guys,

 

Using FTP to get some files and input their contents into arrays, and then doing things based on the output of the arrays.

 

The way it works at the moment is i connect to the remote server through FTP and then download the file to my local server, then open and manipulate it as needed.

 

However is it possible to open the file on the remote server, and then store the contents of it into an array for use on the local machine? Just wondering because this may become a 'free available space' issue and i don't want my script exploding because there is not enough room to write the file locally.

 

Any suggestions?

Cheers

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/
Share on other sites

fopen

 

<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:[email protected]/somefile.txt", "w");
?> 

 

fread

 

<?php
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
?> 

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262545
Share on other sites

Thanks for the reply,

 

I have tested that code previously and could never open the file. I have to connect and save the file locally in order for it to work. I am quite sure it has something to do with not have root access through the FTP (which is a good thing).

 

$handle = fopen("ftp://username:[email protected]/path/to/my/dir/file.txt", "w") or die("Can't open external file");

 

Now what i may think the problem is, is when i connect to the FTP server, by default i connect to /home/user/

 

Does this mean in reality the code is:

 

$handle = fopen("ftp://username:[email protected]/home/user/path/to/my/dir/file.txt", "w") or die("Can't open external file");

 

Therefore the directory is not correct?

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262548
Share on other sites

hummm

ftp://username:[email protected]:aaa.bbb.ccc.ddd/path/to/my/dir/file.txt

that aaa.bbb.ccc.ddd should be the port!

 

ie

ftp://user:[email protected]:21/images/me.jpg

:21 isn't needed

 

your best bet is not to guess,

paste your line into the browser and see what pops up

start at the domain then add to it

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262551
Share on other sites

Sorry about that, fixed my example above.

 

Ok tested it in the browser, and

ftp://username:[email protected]

does actually put me in my home directory, not "/".

 

In my browser if i use the ../../ commands <b>after</b> i have logged in, i can change to the "/" directory, and then move up to my required directory.

 

The address in the browser then looks like this:

 

ftp://domain.com/../../path/to/my/required/directory/file.txt

 

However if i try paste the code in like this for when i log in:

 

ftp://username:[email protected]/../../path/to/my/required/directory/file.txt

 

I get a file not found.

 

Is there any way to get around this?

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262553
Share on other sites

Yeah it does work through a browser. Just not through PHP. It's as though PHP does not understand the ../../.

 

The error logs just show

 

fopen(ftp://username:[email protected]/../../path/to/my/requried/directory/file.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Success in /var/www/html/home/my/userProfiles/index.php on line 24, referer: http://www.domain.com/my/formResults.php

 

formResults.php being the file i am running.

 

Running the exact FTP command im using in the script works fine in the browser.

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262559
Share on other sites

Hummm, is the username and password required?

 

as i would like to test it myself, if you can change the username/password or setup a new one for me to test i'll be happy to try myself,

 

the reason i ask is because i am kinda shooting in the dark at the moment!

 

 

edit: feel free to PM me

Link to comment
https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262561
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.