2

I want to get the url of each file in certain directory

i tried string concatenation (like: domain.folder1.folder2.file.mp3) but some folders and files is with arabic characters that make error when using the url.

example:

this is my code output:

String A :

https://linkimage2url.com/apps/quran full/محمد صديق المنشاوي/تسجيلات الإذاعة المصرية/009 - At-Taubah (The Repentance) سورة التوبة.mp3

this code is not working in some android devices

but the next code works with all devices

String B:

https://linkimage2url.com/apps/quran%20full/%D9%85%D8%AD%D9%85%D8%AF%20%D8%B5%D8%AF%D9%8A%D9%82%20%D8%A7%D9%84%D9%85%D9%86%D8%B4%D8%A7%D9%88%D9%8A/%D8%AA%D8%B3%D8%AC%D9%8A%D9%84%D8%A7%D8%AA%20%D8%A7%D9%84%D8%A5%D8%B0%D8%A7%D8%B9%D8%A9%20%D8%A7%D9%84%D9%85%D8%B5%D8%B1%D9%8A%D8%A9/009%20-%20At-Taubah%20(The%20Repentance)%20%D8%B3%D9%88%D8%B1%D8%A9%20%D8%A7%D9%84%D8%AA%D9%88%D8%A8%D8%A9.mp3

Note: i got String B from internet download manager that converts it automatically when i tried to use string A

my question is:

how to convert String A to String B by php

and is there better way to readdir and get the url of each file

My code is:

if(is_dir($parent)){
if($dh = opendir($parent)){
while(($file = readdir($dh)) != false){

if($file == "." or $file == ".."){
//...
} else { //create object with two fields

sort($file);
$fileName = pathinfo($file)['filename'];

if(is_dir($parent."/".$file)){
$data[] = array('name'=> $fileName, 'subname'=> basename($path), 'url'=> $path."/".$file, "directory"=> true);
} else {
$res = "https://linkimage2url.com".$path."/".$file;
$data[] = array('name'=> $fileName, 'subname'=> basename($path), 'url'=> $res , "directory"=> false);
}

5
  • have you tried Rawulrencode()?
    – Dharman
    Jun 1, 2019 at 8:51
  • yes but the output is different than Strig B and doesnt work
    – Ahmed Gabr
    Jun 1, 2019 at 8:53
  • i get String B from internet download manager that converts it automatically when i tried to use string A
    – Ahmed Gabr
    Jun 1, 2019 at 8:54
  • try htmlentities php.net/manual/en/function.htmlentities.php
    – Auris
    Jun 1, 2019 at 8:59
  • i tried urlencode , rawurlencode , htmlentities , htmlspecial char. not working
    – Ahmed Gabr
    Jun 1, 2019 at 9:05

2 Answers 2

1

Try this one, I've used once for a legacy project:

function encode_fullurl($url) {
$output = '';
$valid  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~!*\'();:@&=+$,/?#[]%';
$length = strlen($url);
for ($i = 0; $i < $length; $i++) {
    $character = $url[$i];
    $output   .= (strpos($valid, $character) === false ? rawurlencode($character) : $character); 
}
return $output;

}


$url ="https://linkimage2url.com/apps/quran full/محمد صديق المنشاوي/تسجيلات الإذاعة المصرية/009 - At-Taubah (The Repentance) سورة التوبة.mp3";
echo encode_fullurl($url);

output:

https://linkimage2url.com/apps/quran%20full/%D9%85%D8%AD%D9%85%D8%AF%20%D8%B5%D8%AF%D9%8A%D9%82%20%D8%A7%D9%84%D9%85%D9%86%D8%B4%D8%A7%D9%88%D9%8A/%D8%AA%D8%B3%D8%AC%D9%8A%D9%84%D8%A7%D8%AA%20%D8%A7%D9%84%D8%A5%D8%B0%D8%A7%D8%B9%D8%A9%20%D8%A7%D9%84%D9%85%D8%B5%D8%B1%D9%8A%D8%A9/009%20-%20At-Taubah%20(The%20Repentance)%20%D8%B3%D9%88%D8%B1%D8%A9%20%D8%A7%D9%84%D8%AA%D9%88%D8%A8%D8%A9.mp3

it is not very performing, but it should do what you need

2
1

this code worked for me

thanks every one



$res1 = "https://linkimage2url.com" .$path."/".$file;
$query = flash_encode ($res1);
$url = htmlentities($query);



 function flash_encode($string)
   {
      $string = rawurlencode($string);
      $string = str_replace("%2F", "/", $string);
      $string = str_replace("%3A", ":", $string);
      return $string;
   }

2
  • get a look to my answer, I think it works and more complete than this one :-) in any case, I'm glad to help and that you solved it
    – icy
    Jun 1, 2019 at 9:58
  • <?php echo '<a href="' . rawurlencode("https://linkimage2url.com/apps/quran full/محمد صديق المنشاوي/تسجيلات الإذاعة المصرية/009 - At-Taubah (The Repentance) سورة التوبة.mp3") . '">' . htmlentities("https://linkimage2url.com/apps/quran full/محمد صديق المنشاوي/تسجيلات الإذاعة المصرية/009 - At-Taubah (The Repentance) سورة التوبة.mp3", ENT_QUOTES, "UTF-8") . '</a>'; ?> may solve your question?
    – Alessandro
    Jun 1, 2019 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.