Jump to content

[SOLVED] Encoding URLS


hellonoko

Recommended Posts

:'(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

 

Link to comment
https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/
Share on other sites

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>";

Link to comment
https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802386
Share on other sites

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;
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/152796-solved-encoding-urls/#findComment-802395
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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