我正在尝试使用PHP上传图像,但图像未保存在服务器上的目录中。但是我能够在数据库中保存图像的路径。请帮忙。这是一段代码。我在网页上没有收到任何错误。
<?php
error_reporting(E_ALL);ini_set('display_errors', 'On');
session_start();
$logged_user_name = $_SESSION['user_name'];
$logged_user_type = $_SESSION['user_type'];
$logged_user_team_id = $_SESSION['team_id'];
$logged_user_team_name = $_SESSION['team_name'];
$uploaded_profile_image = $_POST['propic'];
//$uploaded_profile_image = $_FILES['propic']['name'];
include_once("classes/doEverything_framework.php");
function upload_image()
{
$db_connection_obj = new database_connection;
$db_connection = ($db_connection_obj -> open_database_connection());
global $logged_user_name;
global $uploaded_profile_image;
$profile_image_upload_dir = 'images/uploaded_profile_pics/';
if ($uploaded_profile_image != null || $uploaded_profile_image != "")
{
//file_put_contents($uploaded_profile_image);
move_uploaded_file($uploaded_profile_image, $profile_image_upload_dir);
//file_put_contents($uploaded_profile_image,$profile_image_upload_dir);
$uploaded_profile_image_link = $profile_image_upload_dir.$uploaded_profile_image;
$sql = "UPDATE user_login_table SET user_profile_image = '$uploaded_profile_image_link' WHERE user_name = '$logged_user_name'";
mysql_query($sql, $db_connection);
}
$db_connection_obj -> close_database_connection($db_connection);
}
?>
HTML代码:
<form enctype="multipart/form-data" name="uploadprofileimage" onsubmit="" action="" method="post">
<input type="file" name="propic" id="propic" onclick="" >
<input type="submit" value="Upload" name="upload" id="submit" >
<br>
<label for="propic" id="picerrorlabel"></label>
</form>
<?php
if(isset($_POST['upload'])) //This ensures the function runs only when the submit button is clicked.
{
upload_image();
}
答案 0 :(得分:0)
访问所选文件的正确方法如下
$uploaded_profile_image = $_FILES['propic']['name'];
确保您在php.ini(upload_max_filesize)中的配置文件大小限制内上传文件。休息一切都应该有效。
答案 1 :(得分:0)
更正1: -
您只传递move_uploaded_dir中的目录名称。我认为你应该通过完整的图像路径。并且$uploaded_profile_image
应该是您的图片tmp_name
。
move_uploaded_file($_FILES['propic']['tmp_name'], $profile_image_upload_dir.$uploaded_profile_image);
更正2: - 您无法在$ _POST中获取图片名称。所以它应该是
$uploaded_profile_image = $_FILES['propic']['name'];
答案 2 :(得分:0)
您无法在$ _POST ['propic']中获取图片或任何文件;访问文件或图像的正确方法是使用$ _FILES
所以你应该在move_uploaded_file函数中使用$ uploaded_profile_image = $ _FILES ['propic'] ['name']。请务必检查php.ini中的upload_max_filesize限制。
答案 3 :(得分:0)
要在php上传文件,请使用move_uploaded_file()
$path="upload/".$_FILES["file"]["name"]; // This specifies the path to save file
move_uploaded_file($_FILES["file"]["tmp_name"],$path);
答案 4 :(得分:0)
首先,您无法在POST方法中获取文件值,因此您需要使用$ _FILE来获取文件。所以你需要用这个替换第5行:
$uploaded_profile_image = $_FILES['propic'];
您的代码中的另一个错误是移动上传文件,其中源params应该是文件的临时位置:
move_uploaded_file($uploaded_profile_image['tmp_name'], $profile_image_upload_dir.$uploaded_profile_image['name']);
现在在第27行,您可以通过这种方式获取文件名存储在数据库中:
$uploaded_profile_image_link = $profile_image_upload_dir.$uploaded_profile_image['name'];