/    Sign up×
Community /Pin to ProfileBookmark

Image upload php code

Hello! I am only learning basics in php I wanted to use code that I found online for uploading images into a folder “upload” but the code I found limits the size of the file I don’t understand the code very well and I would ask you to change it for me and explain how its done. I changed the values of acceptable size of the file but when I tried to run it, it gave me errors
This is the code:

[CODE]<?php
if ($_FILES[“file”][“error”] > 0)
{
echo “Error: ” . $_FILES[“file”][“error”] . “<br>”;
}
else
{
echo “Upload: ” . $_FILES[“file”][“name”] . “<br>”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br>”;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” kB<br>”;
echo “Stored in: ” . $_FILES[“file”][“tmp_name”];
}
?>

<?php
$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
$extension = end(explode(“.”, $_FILES[“file”][“name”]));
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/jpg”)
|| ($_FILES[“file”][“type”] == “image/png”))
&& ($_FILES[“file”][“size”] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Error: ” . $_FILES[“file”][“error”] . “<br>”;
}
else
{
echo “Upload: ” . $_FILES[“file”][“name”] . “<br>”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br>”;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” kB<br>”;
echo “Stored in: ” . $_FILES[“file”][“tmp_name”];
}
}
else
{
echo “Invalid file”;
}
?>

<?php
$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
$extension = end(explode(“.”, $_FILES[“file”][“name”]));
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/jpg”)
|| ($_FILES[“file”][“type”] == “image/png”))
&& ($_FILES[“file”][“size”] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Return Code: ” . $_FILES[“file”][“error”] . “<br>”;
}
else
{
echo “Upload: ” . $_FILES[“file”][“name”] . “<br>”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br>”;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” kB<br>”;
echo “Temp file: ” . $_FILES[“file”][“tmp_name”] . “<br>”;

if (file_exists(“avatar/” . $_FILES[“file”][“name”]))
{
echo $_FILES[“file”][“name”] . ” already exists. “;
}
else
{
move_uploaded_file($_FILES[“file”][“tmp_name”],
“avatar/” . $_FILES[“file”][“name”]);
echo “Stored in: ” . “avatar/” . $_FILES[“file”][“name”];
}
}
}
else
{
echo “Invalid file”;
}
?>[/CODE]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 15.2013 — Looks like you just need to change this number:
&amp;&amp; ($_FILES["file"]["size"] &lt; [COLOR="#FF0000"]20000[/COLOR])

(Note however that there are general PHP settings and web server settings that may limit this, too.)
Copy linkTweet thisAlerts:
@corkalom25authorApr 15.2013 — but it occurs in 2 places

btw NogDog I was reading your post on how to display an image in php helped me a lot ?
Copy linkTweet thisAlerts:
@NogDogApr 15.2013 — Try changing both? ?
Copy linkTweet thisAlerts:
@corkalom25authorApr 15.2013 — does the job now ty
×

Success!

Help @corkalom25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...