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
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:password@example.com/somefile.txt", "w");
?> 

 

fread

 

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

Link to comment
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:password@domain.com/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:password@domain.com/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
Share on other sites

Sorry about that, fixed my example above.

 

Ok tested it in the browser, and

ftp://username:password@domain.com

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:password@domain.com/../../path/to/my/required/directory/file.txt

 

I get a file not found.

 

Is there any way to get around this?

Link to comment
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:password@domain.com/../../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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.