View Full Version : Loading file name and file contents to variables from a URL

sir pannels
05-03-2004, 11:54 AM

Ok I don't know how 'do-a-ble' this is so tell me what ya think.. ill explain the basics...

I need to find a free script.. or develop one myself (with some pointers from CF gods) that will go to a url.. one that I set it to go to...
Once its there.. it needs to take the filename.. so after the last '/' in the url and before the .ext... and put that word(s) into a variable.. and then take whats in the file.. and put that into a variable.. so I can access them later on in the script.. once Ive got them i will be manipulating then dymanicly displaying those variables - the manipulating and displaying I can do fine.. its the going to the url and setting those 2 variables first of all...

Is this gonna be amazinly complex? anyone have any points/links/ideas to help me?

Thanks :)
Sir P

05-03-2004, 03:36 PM
Perhaps an example would help.

I wonder what prevents the typical use of ?var=value url var style for this application.

Also if you get files on your server based on url (or any user provided info) security issues must be taken into account.

I use a switch statement and test my url vars for known good values.
This allows me to safely ignore any hacked vars a user submits in an attempt to jump dirs or whatever else the current 'url hack of the day' is.

I'm not saying your approach is wrong. I just would like to know more in order to be able to help.

sir pannels
05-03-2004, 03:45 PM
hey darknight,
cheers for your reply.

Well security isnt really an issue.. this is just a private feature that i plan on using - not used or even accessed by public visitors...

I cant use root paths for this as I want to connect across my servers so i need to use http addresses...
$var's in urls are fine.. i can use these.. just not to get the content of the file you know?

I dont know how best to make an example but i will try and describe it again...
if there is a text files at www.domain.com/files/foo.txt - i would enter that url into my script... i can do that.. just using a form, ill send it to let say $thisaddress ... now what the script needs to do is split up the file from the url.. so get the file name after the last /... so it should get foo.txt now i need it to remove the file extension so we are left with "foo" and set a variable.. lets say $name.. and set it as "foo"... and now grab everything within the file and put it in a variable called $content .. so all im left with is 2 variables .. $name and $content which would are holding foo and the file content.

Does that make clearer sense? or did i ramble?

Cheers, thanks for your time.
Sir P

05-03-2004, 04:37 PM
You mean something like this...

$url = "http://mysite.com/path/to/my/file.ext";
$file = basename($url);
list($name, $ext) = split(".",$file);


$file = "file.ext"
$name= "file"
$ext = ".ext"

Or am I not getting it???

sir pannels
05-03-2004, 04:57 PM
Hey bcarl314,

yeh I spose that is the first part, the bit that sorts out the file name.. however when i run it and call $name or $ext nothing is returned?

there are 2 parts tho, that part - which yeh u did understand... and pretty much sorted.. but theres also the matter of getting the contents of the file and putting that in a variable? anyway ideas bcarl314?

Sir P

05-03-2004, 06:37 PM
Aha, I think I now understand....

Try this...

$fh = fopen($url,"r");
while($data = fgets($fh)) {

All the file information will be in the $d var, just assign it to what ever var you want.

$name = $d;

Does that do the trick???

sir pannels
05-03-2004, 08:18 PM
thats perfect bcarl314 thanks very much works great!

still having problem with splitting the file from the file ext.. im calling it with $name which includes the ext.. i tried using $file.. but that brings back nothing.

Any idea?
Thanks :)

sir pannels
05-03-2004, 11:10 PM
I starded at it for hours... i mean hours lol and ive sorted it now.. thanks everyone that helped!