rhyspaterson Posted May 27, 2007 Share Posted May 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/ Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262545 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262548 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 hummm ftp://username:password@domain.com:aaa.bbb.ccc.ddd/path/to/my/dir/file.txt that aaa.bbb.ccc.ddd should be the port! ie ftp://user:pass@127.0.0.1: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 Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262551 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262553 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 you can not navigate outside the public_html folder from a http steam if its ftp your need to check you access rights on that login.. Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262556 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 But i can navigate to the file through the browser, i just can't connect to it straight away. Navigating to the file is the only thing that works =/ Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262557 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 navigate to the file, then copy the url add the username:password@ then add the file name to the end, thats should be it! Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262558 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262559 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262561 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 Or... post your code (remove user&pass) and i'll test it on my FTP of course i havn't had a problem but their maybe something in your code causing the problem! Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262563 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 The exact code is literally $handle = fopen("ftp://username:password@domain.com/../../path/to/my/required/directory/file.txt", "w") or die ("Can't open external file"); I just dont think PHP passes the /../../ correctly. Thanks so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262565 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 the browser wouldn't have a url with ../../ it would show the absolute URL, as for php using ../../ i have done some tests and it seams fine w/out ../../ my tests have worked fine but without more infomation i can't really help Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262572 Share on other sites More sharing options...
rhyspaterson Posted May 27, 2007 Author Share Posted May 27, 2007 Thanks again. It may be a server-side setting. I will just have to do it the long way for now =/ Quote Link to comment https://forums.phpfreaks.com/topic/53146-fopen-external-files/#findComment-262584 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.