Khi nói đến việc tải lên tệp, cách tốt nhất để thực hiện, sẽ là tải tệp lên máy chủ tệp và lưu đường dẫn của tệp trong cơ sở dữ liệu. Trong trường hợp này, tôi đã sử dụng PHP để tải tệp lên thư mục và lưu đường dẫn của tệp trong cơ sở dữ liệu MySQL. Về cơ bản, bạn sẽ cần một biểu mẫu HTML đơn giản và mã PHP để tải lên tệp. Vì vậy, ở đây, hình thức HTML. Và đây là mã PHP. Xin lưu ý, tôi cũng đã thêm logic để kiểm tra xem tệp đã có trong thư mục đích trước khi tải lên. Vì vậy, đó là nó cho hướng dẫn này. Chơ để biết thêm.
Bài trước làm thế nào để tạo biểu mẫu đăng nhập đơn giản bằng Java Servlet và MySQL DBPost How To Create A Simple Login Form Using Java Servlet And MySQL DB
Tiếp theo đăng những điều cần tránh làm trên phương tiện truyền thông xã hộiPost Things to Avoid Doing on Social Media
Có ai biết bất kỳ hướng dẫn tốt nào về cách tải lên tệp với PHP và lưu đường dẫn tệp vào máy chủ SQL không?
Khi được hỏi ngày 21 tháng 5 năm 2010 lúc 2:41May 21, 2010 at 2:41
Để tải lên một tệp, bạn cần ít nhất một mẫu bài đăng HTML với mã hóa multipart/form-data. Trong đó bạn đặt một trường <?php $fileExistsFlag = 0; $fileName = $_FILES['Filename']['name']; $link = mysqli_connect("localhost","root","","fileupload") or die("Error ".mysqli_error($link)); /* * Checking whether the file already exists in the destination folder */ $query = "SELECT filename FROM filedetails WHERE filename='$fileName'"; $result = $link->query($query) or die("Error : ".mysqli_error($link)); while($row = mysqli_fetch_array($result)) { if($row['filename'] == $fileName) { $fileExistsFlag = 1; } } /* * If file is not present in the destination folder */ if($fileExistsFlag == 0) { $target = "files/"; $fileTarget = $target.$fileName; $tempFileName = $_FILES["Filename"]["tmp_name"]; $fileDescription = $_POST['Description']; $result = move_uploaded_file($tempFileName,$fileTarget); /* * If file was successfully uploaded in the destination folder */ if($result) { echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded"; $query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')"; $link->query($query) or die("Error : ".mysqli_error($link)); } else { echo "Sorry !!! There was an error in uploading your file"; } mysqli_close($link); } /* * If file is already present in the destination folder */ else { echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again."; mysqli_close($link); } ?>0 để duyệt tệp và nút gửi để gửi biểu mẫu.
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form>Trong <?php $fileExistsFlag = 0; $fileName = $_FILES['Filename']['name']; $link = mysqli_connect("localhost","root","","fileupload") or die("Error ".mysqli_error($link)); /* * Checking whether the file already exists in the destination folder */ $query = "SELECT filename FROM filedetails WHERE filename='$fileName'"; $result = $link->query($query) or die("Error : ".mysqli_error($link)); while($row = mysqli_fetch_array($result)) { if($row['filename'] == $fileName) { $fileExistsFlag = 1; } } /* * If file is not present in the destination folder */ if($fileExistsFlag == 0) { $target = "files/"; $fileTarget = $target.$fileName; $tempFileName = $_FILES["Filename"]["tmp_name"]; $fileDescription = $_POST['Description']; $result = move_uploaded_file($tempFileName,$fileTarget); /* * If file was successfully uploaded in the destination folder */ if($result) { echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded"; $query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')"; $link->query($query) or die("Error : ".mysqli_error($link)); } else { echo "Sorry !!! There was an error in uploading your file"; } mysqli_close($link); } /* * If file is already present in the destination folder */ else { echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again."; mysqli_close($link); } ?>1, tệp được tải lên có thể truy cập bằng <?php $fileExistsFlag = 0; $fileName = $_FILES['Filename']['name']; $link = mysqli_connect("localhost","root","","fileupload") or die("Error ".mysqli_error($link)); /* * Checking whether the file already exists in the destination folder */ $query = "SELECT filename FROM filedetails WHERE filename='$fileName'"; $result = $link->query($query) or die("Error : ".mysqli_error($link)); while($row = mysqli_fetch_array($result)) { if($row['filename'] == $fileName) { $fileExistsFlag = 1; } } /* * If file is not present in the destination folder */ if($fileExistsFlag == 0) { $target = "files/"; $fileTarget = $target.$fileName; $tempFileName = $_FILES["Filename"]["tmp_name"]; $fileDescription = $_POST['Description']; $result = move_uploaded_file($tempFileName,$fileTarget); /* * If file was successfully uploaded in the destination folder */ if($result) { echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded"; $query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')"; $link->query($query) or die("Error : ".mysqli_error($link)); } else { echo "Sorry !!! There was an error in uploading your file"; } mysqli_close($link); } /* * If file is already present in the destination folder */ else { echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again."; mysqli_close($link); } ?>2 với tên trường là khóa.
$file = $_FILES['file'];Bạn có thể lấy tên của nó như sau:
$name = $file['name'];Bạn cần di chuyển nó đến một vị trí cố định bằng cách sử dụng <?php $fileExistsFlag = 0; $fileName = $_FILES['Filename']['name']; $link = mysqli_connect("localhost","root","","fileupload") or die("Error ".mysqli_error($link)); /* * Checking whether the file already exists in the destination folder */ $query = "SELECT filename FROM filedetails WHERE filename='$fileName'"; $result = $link->query($query) or die("Error : ".mysqli_error($link)); while($row = mysqli_fetch_array($result)) { if($row['filename'] == $fileName) { $fileExistsFlag = 1; } } /* * If file is not present in the destination folder */ if($fileExistsFlag == 0) { $target = "files/"; $fileTarget = $target.$fileName; $tempFileName = $_FILES["Filename"]["tmp_name"]; $fileDescription = $_POST['Description']; $result = move_uploaded_file($tempFileName,$fileTarget); /* * If file was successfully uploaded in the destination folder */ if($result) { echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded"; $query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')"; $link->query($query) or die("Error : ".mysqli_error($link)); } else { echo "Sorry !!! There was an error in uploading your file"; } mysqli_close($link); } /* * If file is already present in the destination folder */ else { echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again."; mysqli_close($link); } ?>3, nếu không nó sẽ bị mất:
$path = "/uploads/" . basename($name); if (move_uploaded_file($file['tmp_name'], $path)) { // Move succeed. } else { // Move failed. Possible duplicate? }Bạn có thể lưu trữ đường dẫn trong cơ sở dữ liệu theo cách thông thường:
$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')"; // ...Đã trả lời ngày 21 tháng 5 năm 2010 lúc 2:54May 21, 2010 at 2:54
BaluscbaluscBalusC
1.1M366 Huy hiệu vàng3575 Huy hiệu bạc3526 Huy hiệu đồng366 gold badges3575 silver badges3526 bronze badges
6
Từ //www.w3schools.com/php/php_file_upload.asp
HTML
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>PHP
<?php if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; //<- This is it } } ?>Lưu ý rằng để tải lên tệp, bạn cần chỉ định đường dẫn để lưu tệp. Nếu bạn lưu tệp, bạn đã biết đường dẫn của nó.
Đã trả lời ngày 21 tháng 5 năm 2010 lúc 2:53May 21, 2010 at 2:53
BenbenBen
15.9k8 Huy hiệu vàng42 Huy hiệu bạc62 Huy hiệu Đồng8 gold badges42 silver badges62 bronze badges