Upload File Using PHP - Example For Image
Create The HTML Form
Create an HTML form, so you can choose file want to upload:
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file:
<input type="file" name="uploadFile" accept="image/*" />
<br>
<input type="submit" value="Upload File" />
</form>
Create an HTML form, so you can choose file want to upload:
***********************
Code
***********************
Please choose a file:
<input type="file" name="uploadFile" accept="image/*" />
<br>
<input type="submit" value="Upload File" />
</form>
***********************
End of Code
***********************
On the Above code accept is use for accepting only Image file as a File Input. If no accept is specify, All File are accepted.
Create a upload.php file , whose code will be as Follow:
***********************
Code
***********************
?php
$target_dir = "uploads/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;
// Check if file already exists
if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($uploadFile_size > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Only GIF files allowed
if (!($uploadFile_type == "image/gif")) {
echo "Sorry, only GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk==0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
$target_dir = "uploads/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;
// Check if file already exists
if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($uploadFile_size > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Only GIF files allowed
if (!($uploadFile_type == "image/gif")) {
echo "Sorry, only GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk==0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
You did well on inspiring writers like me. Please keep on doing such content.
ReplyDeletePHP Training Center in Chennai
PHP Course Chennai
I was very pleased to find this site.I want to thank you for this great content!! I enjoyed every little bit of it and I have you bookmarked to check out new stuff you post.
ReplyDeleteLoadrunner training institute in Chennai | Qtp course in Chennai
I was really excited about your daily updates. If you have new update me.
ReplyDeleteandroid courses in chennai
android mobile apps development training in chennai
I found your post while searching for some related information on blog search... Its a great blog, keep posting and update the information.
ReplyDeleteFrench Course in Chennai
French Language Classes in Chennai
best german classes in chennai
It's extraordinarily profitable site for learn. This present information are to a great degree valuable to us. It will improve my knowledge. Grateful to you for sharing this superb site.
ReplyDeleteArticle Submission sites | Latest Updates | Technology
Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
ReplyDeletedigital marketing course in coimbatore
php training in coimbatore
Thanks for providing wonderful information with us. Thank you so much.
ReplyDeleteRegards,
PHP Training in Chennai | PHP Course in Chennai
Good resources.
ReplyDeleteThanks for sharing
Java Tutorials | Learn Java
Well organized and easy to understand programming tutorials with lots of examples of why,what and how to program in Java and OOPs