Warptweet Posted October 14, 2007 Share Posted October 14, 2007 I've been searching for a tutorial that shows me how to upload a file from a link instead of from your computer. Does anyone know a tutorial that tells me how to do this? Any help is appreciated. Thanks, -Warptweet Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/ Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 I don't know of a tutorial, but I can tell you some functions to look into to do it... You could use either fsockopen and send headers or you could try file_get_contents to grab the contents of the url entered in a $_POST or $_GET key (from a form, of course). Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368972 Share on other sites More sharing options...
Cagecrawler Posted October 14, 2007 Share Posted October 14, 2007 You don't, at least not in the normal way. Use fopen to turn the url into a stream, then read it's contents and write those into a file on your server. Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368973 Share on other sites More sharing options...
Warptweet Posted October 14, 2007 Author Share Posted October 14, 2007 Wouldn't that return an access denied or forbidden error for trying to open a file not on my webserver? What if it's something like a .swf flash file, not something with text inside? Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368977 Share on other sites More sharing options...
Cagecrawler Posted October 14, 2007 Share Posted October 14, 2007 You need to have allow_url_fopen set to true in your config. fopen and fread work with binary as well as text data, so the filetype doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368980 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 Yeah, like cagecrawler said, you technically aren't uploading a file from the url, you're telling the server the url for it to download. I forgot about fopen.... That would be much faster than fsock open ;p (and you wouldn't have to parse the url down to its host name). I would use file_get_contents if I was going to use fopen though. Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368984 Share on other sites More sharing options...
Warptweet Posted October 14, 2007 Author Share Posted October 14, 2007 www.warptweet.com/url.php I tries using file_get_contents to get a .swf file (a flash game file) from a gaming website to test it on. As you can see, it says "File is not writable". Here is my code. $filename = 'test.txt'; $somecontent = "Add this to the file\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } Anyone have a way around this? I figured that I could copy the binary/text information taken from file_get_contents from a .swf file, put it on the file, and save it as .swf. This apparently does not work. Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368998 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 Return Values Returns TRUE if the filename exists and is writable. Unless the file exists (this is going on what I understand from what I just read in the php manual. I might have misread it.), that function will always return false. Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-368999 Share on other sites More sharing options...
Warptweet Posted October 14, 2007 Author Share Posted October 14, 2007 Thanks, it worked. =) Quote Link to comment https://forums.phpfreaks.com/topic/73155-solved-upload-from-a-link/#findComment-369002 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.