hellonoko Posted April 6, 2009 Share Posted April 6, 2009 :'(y URLS with spaces fail to be processed by fopen fread fwrite etc. EX: www.trashmenagerie.com/audio/Freak You/Freak You - From Nowhere.mp3 However if I paste this link into my browser it becomes: http://www.trashmenagerie.com/audio/Freak%20You/Freak%20You%20-%20From%20Nowhere.mp3 And works fine. Which of the url encoding functions do I need to be using to fix these urls before I process them? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/ Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 urlencode Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802357 Share on other sites More sharing options...
hellonoko Posted April 6, 2009 Author Share Posted April 6, 2009 Cool but that makes my links into something like this: http%3A%2F%2Fwww.trashmenagerie.com%2Fmixes%2Faudio%2Fwholesick%2FNOVEMBERMIXTAPE.mp3 And that does not work. Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802382 Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 You only encode the string that you're adding. www.trashmenagerie.com/audio/Freak You/Freak You - From Nowhere.mp3 If that's the link, then at some point you've added 'Freak You - From Nowhere.mp3' $url = "www.trashmenagerie.com/audio/Freak You/"; $file = urlencode("Freak You - From Nowhere.mp3"); echo "<a href=\"$url$file\">this is the link</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802386 Share on other sites More sharing options...
hellonoko Posted April 6, 2009 Author Share Posted April 6, 2009 I need to take care of that first space though also don't I? /audio/Freak You/ Can I apply it to the full link www.whatever.com and then add the http on? Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802388 Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 Can you show the code you're currently using, it will be easier to suggest solutions for you. urlencode will also change the forward slashes, so you want to encode the relevant sections of uri Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802390 Share on other sites More sharing options...
hellonoko Posted April 6, 2009 Author Share Posted April 6, 2009 Basically the links are in the DB as links without http:// In whatever format they where crawled from a page in but put into the DB with mysql_real_escape_string(); <?php //get the link to copy from the DB $query = "SELECT * FROM `mp3links` WHERE `scraped` = 0 LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($result) or die (mysql_error()); $url = "http://".$row[link]; $last_slash = strripos( $url ,"/"); $clean_file_name = substr( $url , $last_slash + 1 , strlen($url) ); // do a file copy //$url = "http://www.beachtitti.com/summermix_01.mp3"; $dirname = "/home2/sharingi/public_html/scrape/scraped/"; // close th mysql connection because a large file copy may time out mysql mysql_close($connection); $result = copyFile($url , $dirname); ?> <?php function copyFile($url,$dirname) { //$url = urlencode($url); @$file = fopen ($url, "rb"); if (!$file) { echo "Failed to copy $url !"; return false; } else { //copy file $filename = basename($url); $fc = fopen($dirname."$filename", "wb"); while (!feof ($file)) { $line = fread ($file, 1028); fwrite($fc,$line); } fclose($fc); //echo "File $url saved to PC!"; $time_end = microtime(true); $time = $time_end - $time_start; if ( $time > 60 ) { $time = $time / 60; echo "File: $filename coppied in $time minutes"; } else { echo "File: $filename coppied in $time seconds"; } return true; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802395 Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 If you're confident you're only going to be changing whitespace use str_replace; $url = str_replace(' ', '%20', $url); Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802399 Share on other sites More sharing options...
hellonoko Posted April 6, 2009 Author Share Posted April 6, 2009 Hmmm good idea. For now the only error I am getting is on spaces (i think) I can always add str_replaces I suppose. Is there a way I could find out what characters fopen and those functions do not like? Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802402 Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. Quote Link to comment https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802419 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.