如何使用php获取input type="file"中选定文件的路径?
How to get the path of a selected file in input type="file" using php?
我想获取所选文件的路径并将其存储在数据库中以备将来使用。
我有以下代码,但它不起作用。
$path = $_FILES['txtImage']['tmp_name'];
$sql = "INSERT INTO tblquestions (q_category, q_question, q_image, q_correct, q_answer2, q_answer3, q_answer4) VALUES ('$_POST[txtCategory]','$_POST[txtQuestion]','$path','$_POST[txtCorrect]','$_POST[txtChoice2]','$_POST[txtChoice3]','$_POST[txtChoice4]')";
txtImage 是我输入的名称 type="file" 和 $path 是我正在使用的代码,但它不起作用。它只是 returns 空白。
任何人都可以帮助我或引导我使用不同的、希望更简单的方法吗?先感谢您。 :)
PHP 会将提交的文件存储在一个临时目录中,您不应该将其存储在数据库中,因为它会很不稳定。
使用 PHP 的 move_uploaded_file
功能将文件移动到文件系统中您想要的位置,然后将该路径存储在数据库中。
您是否在 HTML 端正确设置了您的表单 enctype,以便它能够正常工作。
其次,TMP 只是一个临时位置,您必须使用 PHP function move_uploaded_file 将该文件移动到服务器可读目录
阅读 enctypes,in this answer or on w3 schools.
<form name="YourForm" method="post" enctype="multipart/form-data" action="">
$tmp_path = $_FILES['txtImage']['tmp_name'];
$dest_path = path_where_in_your_server_you_want_this_image_to_be_moved.$_FILES['textImage']['name']; (eg: 'images/'.$_FILES['name'])
if(move_uploaded_file($tmp_path,$dest_path)){ //this will move the file from tmp location in server to the destination you provide in the second parameter
$sql = "INSERT INTO tblquestions (q_category, q_question, q_image, q_correct, q_answer2, q_answer3, q_answer4) VALUES ('$_POST[txtCategory]','$_POST[txtQuestion]','$dest_path','$_POST[txtCorrect]','$_POST[txtChoice2]','$_POST[txtChoice3]','$_POST[txtChoice4]')";
}else{
echo "Image could not be uploaded"
}
另请注意,上传文件时可能存在权限问题(与您希望将图像上传到的目录有关)。
祝你好运!
我想获取所选文件的路径并将其存储在数据库中以备将来使用。 我有以下代码,但它不起作用。
$path = $_FILES['txtImage']['tmp_name'];
$sql = "INSERT INTO tblquestions (q_category, q_question, q_image, q_correct, q_answer2, q_answer3, q_answer4) VALUES ('$_POST[txtCategory]','$_POST[txtQuestion]','$path','$_POST[txtCorrect]','$_POST[txtChoice2]','$_POST[txtChoice3]','$_POST[txtChoice4]')";
txtImage 是我输入的名称 type="file" 和 $path 是我正在使用的代码,但它不起作用。它只是 returns 空白。
任何人都可以帮助我或引导我使用不同的、希望更简单的方法吗?先感谢您。 :)
PHP 会将提交的文件存储在一个临时目录中,您不应该将其存储在数据库中,因为它会很不稳定。
使用 PHP 的 move_uploaded_file
功能将文件移动到文件系统中您想要的位置,然后将该路径存储在数据库中。
您是否在 HTML 端正确设置了您的表单 enctype,以便它能够正常工作。 其次,TMP 只是一个临时位置,您必须使用 PHP function move_uploaded_file 将该文件移动到服务器可读目录 阅读 enctypes,in this answer or on w3 schools.
<form name="YourForm" method="post" enctype="multipart/form-data" action="">
$tmp_path = $_FILES['txtImage']['tmp_name'];
$dest_path = path_where_in_your_server_you_want_this_image_to_be_moved.$_FILES['textImage']['name']; (eg: 'images/'.$_FILES['name'])
if(move_uploaded_file($tmp_path,$dest_path)){ //this will move the file from tmp location in server to the destination you provide in the second parameter
$sql = "INSERT INTO tblquestions (q_category, q_question, q_image, q_correct, q_answer2, q_answer3, q_answer4) VALUES ('$_POST[txtCategory]','$_POST[txtQuestion]','$dest_path','$_POST[txtCorrect]','$_POST[txtChoice2]','$_POST[txtChoice3]','$_POST[txtChoice4]')";
}else{
echo "Image could not be uploaded"
}
另请注意,上传文件时可能存在权限问题(与您希望将图像上传到的目录有关)。
祝你好运!