create account

php ile dosya indirme işlemini limitlendirme (turkish tutorial) by eresbos

View this thread on: hive.blogpeakd.comecency.com
· @eresbos · (edited)
$30.41
php ile dosya indirme işlemini limitlendirme (turkish tutorial)
English
Hello to everyone my name is eresbos,

- How to create a limit file downloading with php?

Türkçe

Merhaba arkadaşlar ben eresbos.Bugün sizlere php ile dosya indirme işlemlerinde indirme hızı limiti nasıl konulur onu göstereceğim.

- Kodlarımız ;

```
<?php

$yerel_dosya = "deneme.zip";
$indirilecek_dosya = "indirilendosya.zip";

$indirme_hizi = 30;

if(file_exists($yerel_dosya) && is_file($yerel_dosya)) {

	header("Content-Type: application/octet-stream");
	header("Content-Length: ".filesize($yerel_dosya));
	header("Content-Disposition: attachment; filename=" .$indirilecek_dosya);

	flush();

	$dosya = fopen($yerel_dosya, "r");
	while(!feof($dosya)) {

		print fread($dosya, round($indirme_hizi * 1024) );
		flush();
		sleep(1);

	}


	fclose($dosya);

}else {

	echo 'Dosya mevcut degil';

}
?>
```

![kodlar.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855555/gtocghekqoxp9hsqbjaz.png)


1- Projemiz için bir tane dosyaya ihtiyacımız var ben 4.36 mb boyutunda deneme.zip olarak oluşturdum ve localhosta attım.Sizde istediğiniz boyutta ve istediğiniz isimde dosya oluşturup localhosta atabilirsiniz.

![dosya.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514852733/xybbrzuqtu33kn03kjbh.png)

2- Değişkenlerimizi tanımlayalım;
- ```$yerel_dosya = "deneme.zip"``` değişkeni localhostta bulunan dosyamızı tanıtmaya yarıyor.

![yerel_dosya.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853228/ajceimwhlmpu6630dya4.png)

- ```$indirilecek_dosya = "indirilendosya.zip"``` dosyamızın indirme işlemi tamamlandığınca alacağı ismi tanıtmamızı sağlıyor.

![indirilecek_dosya.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853253/tes2nxfdsdl3cjlg3m8e.png)

- ```$indirme_hizi = 30;``` indirme limitimizi belirlememizi sağlıyor.Yani saniyede maksimum 30 kilobyte indirmemizi sağlayacak.

![indirme_hizi.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853364/dbqp0whjxfyzwf6rjbrn.png)

3- Dosya kontrolümüzü yapıyoruz.'Eğer dosyamız varsa ve bu klasör değilse' şeklinde koşulumuzu yazıyoruz.
```
if(file_exists($yerel_dosya) && is_file($yerel_dosya)) {
```
![dosya kontrolu.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853663/hp1cyubr11usfghexkim.png)

4- İndirme işlemini gerçekleştirmek için;
- ```header("Content-Type: application/octet-stream");``` burada indirilecek dosyamızın tipini zip olarak belirliyoruz.
- ```header("Content-Length: ".filesize($yerel_dosya));``` burada indirelecek dosyamızın boyutunu belirliyoruz.
- ```header("Content-Disposition: attachment; filename=" .$indirilecek_dosya);``` burada indirme işlemimizi başlatmak ve indirirken hangi ismi kullanacağımızı belirlemek için yeni bir header yazıyoruz.

```
	header("Content-Type: application/octet-stream");
	header("Content-Length: ".filesize($yerel_dosya));
	header("Content-Disposition: attachment; filename=" .$indirilecek_dosya);
```

![headerlar.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854168/hjgvznnh8xobx7jpku4d.png)

5- ```flush();``` tarayıcıya gömme işlemimizi yapıyoruz.

![gömme.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854349/vqg1szyqngcfrkr1duyc.png)
 
6- ```$dosya = fopen($yerel_dosya, "r");``` dosyamızı açıp parça parça okutma işlemini gerçekleştiriyoruz.

![dosya açma.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854524/v6qylorgdvtovabe9azy.png)

7- ```while(!feof($dosya)) {``` while döngümüzü kuruyoruz, dosyanın sonuna kadar okunup okunmadığını kontrol ediyoruz.

![dosya okuma.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854679/f9zf2oba20e05yb1wjim.png)

8- ```print fread($dosya, round($indirme_hizi * 1024) );``` dosyamızı okuttuktan sonra 1024 ile çarpıp kilobyte değerini ekrana yazdırıyoruz.

![xxx.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854845/gdetatljfd1mjdxqzcik.png)

9- Tekrar ```flush();``` methoduyla tarayıcımıza gömüyoruz.

![gömme.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854940/e04kubvxm3relyjm6rhu.png)

10- ```sleep(1);``` sleep ile 1 saniye bekleme süresi koyuyoruz.

![sleep.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855004/lzocnyw00el7bwzeytof.png)

11- ```fclose($dosya);``` burada indirme işlemini tamamladıktan sonra fclose ile açtığımız dosyayı kapatıyoruz.

![kapatma.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855090/czfhxgyoru9b0b9bgmve.png)

12- ```}else {``` eğer üçüncü adımda yazdığımız koşul sağlanmıyorsa,

![else.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855167/qmeywlnlqas8dbvufir0.png)

13- ```echo 'Dosya mevcut degil';``` ekrana dosya mevcut değil sonucunu bastırıyoruz.

Github link : https://github.com/php/php-src

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@eresbos/php-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoreresbos
permlinkphp-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":1903522,"name":"php-src","full_name":"php/php-src","owner":{"login":"php","id":25158,"avatar_url":"https://avatars2.githubusercontent.com/u/25158?v=4","gravatar_id":"","url":"https://api.github.com/users/php","html_url":"https://github.com/php","followers_url":"https://api.github.com/users/php/followers","following_url":"https://api.github.com/users/php/following{/other_user}","gists_url":"https://api.github.com/users/php/gists{/gist_id}","starred_url":"https://api.github.com/users/php/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php/subscriptions","organizations_url":"https://api.github.com/users/php/orgs","repos_url":"https://api.github.com/users/php/repos","events_url":"https://api.github.com/users/php/events{/privacy}","received_events_url":"https://api.github.com/users/php/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/php/php-src","description":"The PHP Interpreter","fork":false,"url":"https://api.github.com/repos/php/php-src","forks_url":"https://api.github.com/repos/php/php-src/forks","keys_url":"https://api.github.com/repos/php/php-src/keys{/key_id}","collaborators_url":"https://api.github.com/repos/php/php-src/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/php/php-src/teams","hooks_url":"https://api.github.com/repos/php/php-src/hooks","issue_events_url":"https://api.github.com/repos/php/php-src/issues/events{/number}","events_url":"https://api.github.com/repos/php/php-src/events","assignees_url":"https://api.github.com/repos/php/php-src/assignees{/user}","branches_url":"https://api.github.com/repos/php/php-src/branches{/branch}","tags_url":"https://api.github.com/repos/php/php-src/tags","blobs_url":"https://api.github.com/repos/php/php-src/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/php/php-src/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/php/php-src/git/refs{/sha}","trees_url":"https://api.github.com/repos/php/php-src/git/trees{/sha}","statuses_url":"https://api.github.com/repos/php/php-src/statuses/{sha}","languages_url":"https://api.github.com/repos/php/php-src/languages","stargazers_url":"https://api.github.com/repos/php/php-src/stargazers","contributors_url":"https://api.github.com/repos/php/php-src/contributors","subscribers_url":"https://api.github.com/repos/php/php-src/subscribers","subscription_url":"https://api.github.com/repos/php/php-src/subscription","commits_url":"https://api.github.com/repos/php/php-src/commits{/sha}","git_commits_url":"https://api.github.com/repos/php/php-src/git/commits{/sha}","comments_url":"https://api.github.com/repos/php/php-src/comments{/number}","issue_comment_url":"https://api.github.com/repos/php/php-src/issues/comments{/number}","contents_url":"https://api.github.com/repos/php/php-src/contents/{+path}","compare_url":"https://api.github.com/repos/php/php-src/compare/{base}...{head}","merges_url":"https://api.github.com/repos/php/php-src/merges","archive_url":"https://api.github.com/repos/php/php-src/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/php/php-src/downloads","issues_url":"https://api.github.com/repos/php/php-src/issues{/number}","pulls_url":"https://api.github.com/repos/php/php-src/pulls{/number}","milestones_url":"https://api.github.com/repos/php/php-src/milestones{/number}","notifications_url":"https://api.github.com/repos/php/php-src/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/php/php-src/labels{/name}","releases_url":"https://api.github.com/repos/php/php-src/releases{/id}","deployments_url":"https://api.github.com/repos/php/php-src/deployments","created_at":"2011-06-16T01:52:25Z","updated_at":"2017-12-31T12:57:25Z","pushed_at":"2017-12-31T12:04:09Z","git_url":"git://github.com/php/php-src.git","ssh_url":"git@github.com:php/php-src.git","clone_url":"https://github.com/php/php-src.git","svn_url":"https://github.com/php/php-src","homepage":"http://www.php.net","size":305504,"stargazers_count":14041,"watchers_count":14041,"language":"C","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4565,"mirror_url":null,"archived":false,"open_issues_count":133,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":4565,"open_issues":133,"watchers":14041,"default_branch":"master","score":149.17505},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","utopian-io","tr"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855555/gtocghekqoxp9hsqbjaz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514852733/xybbrzuqtu33kn03kjbh.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853228/ajceimwhlmpu6630dya4.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853253/tes2nxfdsdl3cjlg3m8e.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853364/dbqp0whjxfyzwf6rjbrn.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853663/hp1cyubr11usfghexkim.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854168/hjgvznnh8xobx7jpku4d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854349/vqg1szyqngcfrkr1duyc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854524/v6qylorgdvtovabe9azy.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854679/f9zf2oba20e05yb1wjim.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854845/gdetatljfd1mjdxqzcik.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854940/e04kubvxm3relyjm6rhu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855004/lzocnyw00el7bwzeytof.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855090/czfhxgyoru9b0b9bgmve.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855167/qmeywlnlqas8dbvufir0.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855555/gtocghekqoxp9hsqbjaz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514852733/xybbrzuqtu33kn03kjbh.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853228/ajceimwhlmpu6630dya4.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853253/tes2nxfdsdl3cjlg3m8e.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853364/dbqp0whjxfyzwf6rjbrn.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514853663/hp1cyubr11usfghexkim.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854168/hjgvznnh8xobx7jpku4d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854349/vqg1szyqngcfrkr1duyc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854524/v6qylorgdvtovabe9azy.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854679/f9zf2oba20e05yb1wjim.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854845/gdetatljfd1mjdxqzcik.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514854940/e04kubvxm3relyjm6rhu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855004/lzocnyw00el7bwzeytof.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855090/czfhxgyoru9b0b9bgmve.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855167/qmeywlnlqas8dbvufir0.png"],"moderator":{"account":"sedatyildiz","reviewed":true,"pending":false,"flagged":false}}"
created2018-01-02 01:13:21
last_update2018-01-06 22:20:33
depth0
children2
last_payout2018-01-09 01:13:21
cashout_time1969-12-31 23:59:59
total_payout_value21.271 HBD
curator_payout_value9.137 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,904
author_reputation389,195,925,318
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,463,222
net_rshares3,116,497,506,608
author_curate_reward""
vote details (28)
@sedatyildiz · (edited)
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorsedatyildiz
permlinkre-eresbos-php-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial-20180103t035809488z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-03 03:58:12
last_update2018-01-03 04:19:21
depth1
children0
last_payout2018-01-10 03:58:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation4,535,285,897,770
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,689,694
net_rshares0
@utopian-io ·
### Hey @eresbos I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-eresbos-php-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial-20180104t072109438z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-04 07:21:09
last_update2018-01-04 07:21:09
depth1
children0
last_payout2018-01-11 07:21:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,505
author_reputation152,955,367,999,756
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,968,910
net_rshares0