neden farklı formatta indirmek aynı URL kıvırmak değil mi?

6 Cevap php

kıvırmak indir http://mysite.com/Lunacy%20Disc%202%20of%202%20(U)(Saturn).zip

ama

http://mysite.com/Lunacy Disc 2 of 2 (U)(Saturn).zip

Neden bu böyledir?

Ben ilk biçime dönüştürmek gerekir mi?

kullanarak urlencode ($ url) üzerinden oluşturulan URL başarısız olur.

6 Cevap

İki sorun:

  1. urlencode da size bölü kodlar. Bu URL'ler, değil tam URL'ler kullanmak için sorgu dizeleri kodlamak anlatıyor.
  2. urlencode + şeklinde boşluk kodlar. İhtiyacınız rawurlencode if you want spaces as %20.

"Ilk biçimi" için bir URL dönüştürmek için PHP fonksiyonunu urlencode kullanabilirsiniz.


Now, for the "why", the answer can probably be found in the
RFC 1738 - Uniform Resource Locators (URL).

Bazı paragraf alıntı:

Octets must be encoded if they have no corresponding graphic
character within the US-ASCII coded character set, if the use of the
corresponding character is unsafe, or if the corresponding character
is reserved for some other interpretation within the particular URL
scheme.

No corresponding graphic US-ASCII:

URLs are written only with the graphic printable characters of the
US-ASCII coded character set. The octets 80-FF hexadecimal are not
used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent
control characters; these must be encoded.

Bir uzay kod% 20 var - Biraz sonra, bu aralığın 00-1F değil, bu yüzden bu nedenle kodlanmış olmalıdır ... Ama:

Unsafe:

   Characters can be unsafe for a number of reasons.  The space
   character is unsafe because significant spaces may disappear and
   insignificant spaces may be introduced when URLs are transcribed or
   typeset or subjected to the treatment of word-processing programs.

Ve burada, sen boşluk karakteri çok kodlanmış / kaçtı zorundadır neden biliyor ;-)

Sorunun sadece boşluklu ise urlencode() gerçekten, kıvrılma ile başarısız oluyor, bunları el yerini alabilir

$url = str_replace(' ', '%20', $url);

Sen boşluk çevirmek UrlEncode gerekir (sizin örnekte; bunu gerektiren diğer karakterler vardır) internet üzerinden iletimi için. Kodlama çeşitli haberleşme protokolleri feshetme veya aksi takdirde onu ele yaparken dize bozmak yok olmasını sağlar.

2 (U) (Satürn) http://mysite.com/Lunacy Disc 2. Zip

Bu geçerli bir URL değil. Gerekirse en modern tarayıcılar sizin için otomatik olarak url kodlar, çünkü bu gibi erişilmesi adresler tarayıcınızda çalışabilir. Kıvırmak kütüphane bunu otomatik olarak olmamalıdır.

Neden? some characters has special meanings such as # (html anchor). Çünkü

Yani tüm karakterler except alfanumeric olanları ne olursa olsun kodlanmış veya gerekir kodlanmıştır.