Hướng dẫn php upload file and save path to database - php tải lên tệp và lưu đường dẫn đến cơ sở dữ liệu

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.

form method="post" action="upload.php" enctype="multipart/form-data">
    <p>File :</p>
    <input type="file" name="Filename"> 
    <p>Description</p>
    <textarea rows="10" cols="35" name="Description"></textarea>
    <br/>
    <input TYPE="submit" name="upload" value="Submit"/>
</form>

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.

<?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);
	}	
?>

Vì vậy, đó là nó cho hướng dẫn này. Chơ để biết thêm.

Hướng dẫn php upload file and save path to database - php tải lên tệp và lưu đường dẫn đến cơ sở dữ liệu

Hướng dẫn php upload file and save path to database - php tải lên tệp và lưu đường dẫn đến cơ sở dữ liệu

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

Hướng dẫn php upload file and save path to database - php tải lên tệp và lưu đường dẫn đến cơ sở dữ liệu

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

Hướng dẫn php upload file and save path to database - php tải lên tệp và lưu đường dẫn đến cơ sở dữ liệu

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ừ http://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

Làm thế nào chúng tôi lưu tệp tải lên trong cơ sở dữ liệu trong PHP?

Nhận tiện ích mở rộng tệp bằng hàm pathinfo () trong PHP và xác thực định dạng tệp để kiểm tra xem người dùng có chọn tệp hình ảnh hay không. Tải lên hình ảnh lên máy chủ bằng hàm Move_uploaded_file () trong tên tệp hình ảnh php.insert trong cơ sở dữ liệu MySQL bằng PHP. Trạng thái tải lên sẽ được hiển thị cho người dùng.Upload image to server using move_uploaded_file() function in PHP. Insert image file name in the MySQL database using PHP. Upload status will be shown to the user.

Php Store đã tải lên ở đâu?

PHP lưu trữ tất cả các tệp tạm thời, bao gồm các tệp được tải lên, trong thư mục tệp tạm thời như được chỉ định trong php.ini. Lưu ý rằng để tải lên, các tệp đó có thể bị xóa ngay khi tập lệnh, tệp được tải lên đã bị chấm dứt (vì vậy trừ khi bạn trì hoãn tập lệnh đó, có lẽ bạn sẽ không thấy tệp được tải lên).in the temporary files directory as specified in the php. ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file).

Làm cách nào để lưu vị trí tệp trong PHP?

PHP $ target_path = "hình ảnh/";$ target_path = $ target_path.basename ($ _files ['userFile'] ['name']);Move_upLoaded_File ($ _Files ['userFile'] ['tmp_name'], $ target_path);?> Khi tệp (hình ảnh) được lưu tại đường dẫn được chỉ định ... điều gì sẽ xảy ra nếu tôi muốn lưu tệp với một số tên mong muốn ....$target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

Làm thế nào để tải lên tệp trong thư mục cụ thể trong PHP?

Sử dụng Move_upLoaded_File () thay vì sao chép () .... Các tệp được tải lên đi lên thư mục tạm thời và Move_uploaded_file () nhận thức được điều này;Nhưng bạn cũng cần chỉ định thư mục là đường dẫn hệ thống tập tin, không phải là đường dẫn web;và điểm đến nên bao gồm một tên tệp cũng như một đường dẫn.....
Bạn đã không làm phẳng con đường.- Amit Verma ..