In this post I am going to show you how to upload file to server using simple PHP and HTML code. Uploading files with php is one of the most fundamental programming skill. This script will allow you to upload single image file and store it to the specified directory to the server for later usage.
Source:
Source:
<?php
valid_formats = array("jpg","jpeg","JPG", "png", "gif", "bmp");
$max_file_size = 1024*1024*1024;
$dir = "uploads/";
// If File Submitted
if($_SERVER['REQUEST_METHOD'] == "POST"){
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ( in_array($ext, $valid_formats)) {
if( $_FILES['file']['size'] < $max_file_size ){
$uniq = base_convert(uniqid(), 16, 10);
$tmp = $_FILES['file']['tmp_name'];
$uniq_file_name = $uniq.".".$ext;
if(move_uploaded_file($tmp, $dir.$uniq_file_name)){
$query = mysql_query("INSERT INTO files (id, file) VALUES (null, '{$uniq_file_name}')");
$msg = "Uploading successful!";
}
else{
$msg = "Problem while moving file";
}
}
else{
$msg = "File is too large";
}
}
else{
$msg = "Worng file format";
}
}
?>
<html>
<head>
<title>Simple PHP File Uploader</title>
</head>
<body>
<?php
//Show message
if(isset($msg)){
echo "<p>{$msg}</p>\n";
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<label for="file">File:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
If we look over the script we can see that there is two separate part in this script. First one is the PHP and the second one is HTML form. HTML form will allow you to upload file using web browser and the PHP code will catch your uploaded file into the server. It will look up the validation of your upload, give it a unique name and store it to your defined directory,
Hi....
ReplyDeleteHow to keep image file name same as the image original file name?
Thanks before....
Tay phải Vương Lâm đặt lên trán Vương Hạo, hắn trầm tư không nói gì.
ReplyDeleteHoàng Long chân nhân và các vị trưởng bối trong môn phái lúc này cũng đã
tới đây, Hồng Kiểm lão giả nhìn thoáng qua lò luyện đan rồi lại nhìn
Vương Hạo một chút, sắc mặt hắn hơi thay đổi, thất thanh kêu:
- Lô Hỏa Khai Đỉnh.
Đạo Hư cẩn thận nhìn thoáng qua một chút, sau đó lão vứt Lữ Vân Kiệt lên trên mặt đất, lão trầm giọng nói:
học kế toán thực hành
forum rao vặt cattleya
học kế toán tổng hợp
eco-green-city
học kế toán tại bắc ninh
dịch vụ kế toán trọn gói
chung cư hà nội
dịch vụ làm báo cáo tài chính cuối năm
manor central park
dịch vụ làm báo cáo tài chính
kế toán cho quản lý
khoá học kế toán thuế
keny idol
trung tâm kế toán tại long biên
trung tâm kế toán tại hải phòng
- Lữ Vân Kiệt, ngươi thật là to gan, Lô Hỏa Khai Đình tuy không nói là
cấm thuật, nhưng chỉ có thể sử dụng với người ngoài, không thể thi triển
đối với đồng môn, ngươi quên rồi sao?
Sắc mặt Lữ Vân Kiệt không còn chút máu, hắn quỳ rạp trên mặt đất, thấp giọng nói:
- Đệ tử mười lăm tuổi đã lên núi bái làm môn hạ của sư phụ, đến nay đã
trải qua hai mươi lăm năm, tu vi của đệ tử vãn dừng lại ở tầng thứ sáu
của Ngưng Khí kỳ. Đệ tử không cam lòng, Lô Hỏa Khai Đình một khi luyện
máy chạy bộ điện bàn bóng bàn giàn tập tạ
ReplyDeleteشراء اثاث مستعمل بالرياض
ReplyDeleteشراء الاثاث المستعمل بالرياض
شركة شراء اثاث مستعمل بالرياض
شركة شراء الاثاث المستعمل بالرياض
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course