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

View this thread on steempeak.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)
post_id23,029,894
authoreresbos
permlinkphp-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial
categoryutopian-io
json_metadata"{"type": "tutorials", "repository": {"id": 1903522, "watchers": 14041, "events_url": "https://api.github.com/repos/php/php-src/events", "forks": 4565, "name": "php-src", "issues_url": "https://api.github.com/repos/php/php-src/issues{/number}", "trees_url": "https://api.github.com/repos/php/php-src/git/trees{/sha}", "fork": false, "git_url": "git://github.com/php/php-src.git", "assignees_url": "https://api.github.com/repos/php/php-src/assignees{/user}", "size": 305504, "owner": {"id": 25158, "following_url": "https://api.github.com/users/php/following{/other_user}", "starred_url": "https://api.github.com/users/php/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/php/subscriptions", "repos_url": "https://api.github.com/users/php/repos", "login": "php", "gists_url": "https://api.github.com/users/php/gists{/gist_id}", "followers_url": "https://api.github.com/users/php/followers", "received_events_url": "https://api.github.com/users/php/received_events", "type": "Organization", "avatar_url": "https://avatars2.githubusercontent.com/u/25158?v=4", "site_admin": false, "organizations_url": "https://api.github.com/users/php/orgs", "gravatar_id": "", "events_url": "https://api.github.com/users/php/events{/privacy}", "url": "https://api.github.com/users/php", "html_url": "https://github.com/php"}, "forks_count": 4565, "git_refs_url": "https://api.github.com/repos/php/php-src/git/refs{/sha}", "blobs_url": "https://api.github.com/repos/php/php-src/git/blobs{/sha}", "pushed_at": "2017-12-31T12:04:09Z", "watchers_count": 14041, "teams_url": "https://api.github.com/repos/php/php-src/teams", "comments_url": "https://api.github.com/repos/php/php-src/comments{/number}", "archived": false, "svn_url": "https://github.com/php/php-src", "merges_url": "https://api.github.com/repos/php/php-src/merges", "subscribers_url": "https://api.github.com/repos/php/php-src/subscribers", "issue_events_url": "https://api.github.com/repos/php/php-src/issues/events{/number}", "stargazers_url": "https://api.github.com/repos/php/php-src/stargazers", "mirror_url": null, "statuses_url": "https://api.github.com/repos/php/php-src/statuses/{sha}", "has_projects": false, "milestones_url": "https://api.github.com/repos/php/php-src/milestones{/number}", "description": "The PHP Interpreter", "keys_url": "https://api.github.com/repos/php/php-src/keys{/key_id}", "open_issues": 133, "compare_url": "https://api.github.com/repos/php/php-src/compare/{base}...{head}", "ssh_url": "git@github.com:php/php-src.git", "license": {"name": "Other", "key": "other", "url": null, "spdx_id": null}, "html_url": "https://github.com/php/php-src", "commits_url": "https://api.github.com/repos/php/php-src/commits{/sha}", "open_issues_count": 133, "stargazers_count": 14041, "branches_url": "https://api.github.com/repos/php/php-src/branches{/branch}", "full_name": "php/php-src", "forks_url": "https://api.github.com/repos/php/php-src/forks", "score": 149.17505, "deployments_url": "https://api.github.com/repos/php/php-src/deployments", "contributors_url": "https://api.github.com/repos/php/php-src/contributors", "homepage": "http://www.php.net", "contents_url": "https://api.github.com/repos/php/php-src/contents/{+path}", "has_downloads": true, "collaborators_url": "https://api.github.com/repos/php/php-src/collaborators{/collaborator}", "created_at": "2011-06-16T01:52:25Z", "git_commits_url": "https://api.github.com/repos/php/php-src/git/commits{/sha}", "releases_url": "https://api.github.com/repos/php/php-src/releases{/id}", "private": false, "pulls_url": "https://api.github.com/repos/php/php-src/pulls{/number}", "git_tags_url": "https://api.github.com/repos/php/php-src/git/tags{/sha}", "notifications_url": "https://api.github.com/repos/php/php-src/notifications{?since,all,participating}", "language": "C", "updated_at": "2017-12-31T12:57:25Z", "has_wiki": false, "downloads_url": "https://api.github.com/repos/php/php-src/downloads", "hooks_url": "https://api.github.com/repos/php/php-src/hooks", "languages_url": "https://api.github.com/repos/php/php-src/languages", "default_branch": "master", "labels_url": "https://api.github.com/repos/php/php-src/labels{/name}", "url": "https://api.github.com/repos/php/php-src", "has_pages": false, "tags_url": "https://api.github.com/repos/php/php-src/tags", "clone_url": "https://github.com/php/php-src.git", "archive_url": "https://api.github.com/repos/php/php-src/{archive_format}{/ref}", "has_issues": false, "issue_comment_url": "https://api.github.com/repos/php/php-src/issues/comments{/number}", "subscription_url": "https://api.github.com/repos/php/php-src/subscription"}, "pullRequests": [], "format": "markdown", "image": ["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514855555/gtocghekqoxp9hsqbjaz.png"], "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"], "app": "utopian/1.0.0", "moderator": {"flagged": false, "account": "sedatyildiz", "reviewed": true, "pending": false}, "platform": "github", "tags": ["utopian-io", "utopian-io", "tr"], "community": "utopian"}"
created2018-01-02 01:13:21
last_update2018-01-06 22:20:33
depth0
children2
net_rshares3,116,497,506,608
last_payout2018-01-09 01:13:21
cashout_time1969-12-31 23:59:59
total_payout_value21.271 SBD
curator_payout_value9.137 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length4,904
author_reputation389,045,144,994
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
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)
post_id23,229,051
authorsedatyildiz
permlinkre-eresbos-php-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial-20180103t035809488z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2018-01-03 03:58:12
last_update2018-01-03 04:19:21
depth1
children0
net_rshares0
last_payout2018-01-10 03:58:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length172
author_reputation4,387,551,450,260
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@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)
post_id23,472,455
authorutopian-io
permlinkre-eresbos-php-ile-dosya-indirme-islemini-limitlendirme-turkish-tutorial-20180104t072109438z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2018-01-04 07:21:09
last_update2018-01-04 07:21:09
depth1
children0
net_rshares0
last_payout2018-01-11 07:21:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,505
author_reputation152,913,012,544,965
root_title"php ile dosya indirme işlemini limitlendirme (turkish tutorial)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000