将图像路径放入mysql数据库

时间:2014-08-23 14:04:41

标签: php sql path

编辑我的帖子,这样会更清楚。 我已成功获得脚本将图像及其缩略图上传到webserver上指定的文件夹。我想将image path和resized_image路径放入mysql数据库。 我不知道从哪里开始。是upload_image_script.php中必需的一些脚本 或my_file.php?请帮忙,也许有人可以展示某种例子......

upload_image_script.php:

<?php
$fileName = $_FILES["uploaded_file"]["name"]; 
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; 
$fileType = $_FILES["uploaded_file"]["type"];
$fileSize = $_FILES["uploaded_file"]["size"];
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; 
$kaboom = explode(".", $fileName); 
$fileExt = end($kaboom); 
if (!$fileTmpLoc) { 
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 5242880) {
    echo "ERROR: Your file was larger than 5 Megabytes in size.";
    unlink($fileTmpLoc);
    exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {

     echo "ERROR: Your image was not .gif, .jpg, or .png.";
     unlink($fileTmpLoc);
     exit();
} else if ($fileErrorMsg == 1) {
    echo "ERROR: An error occured while processing the file. Try again.";
    exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/original/$fileName");
if ($moveResult != true) {
    echo "ERROR: File not uploaded. Try again.";
    unlink($fileTmpLoc);
    exit();
}
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/original/$fileName";
$resized_file = "uploads/resized/resized_$fileName";
$wmax = 100;
$hmax = 50;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";

 ?>

和my_file.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form name="form" enctype="multipart/form-data" method="POST" action="image_upload_script.php">
  <p>Choose your file:
  <input name="uploaded_file" type="file" />
  </p>
  <p>
    <input type="submit" value="upload" />
    <input type="hidden" name="MM_insert" value="form" />
  </p>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应该在 upload_image_script.php

中执行此操作

首先,您需要创建一个数据库,用于存储图像,然后将代码添加到您的代码中:

$target_file = "uploads/original/$fileName";
$resized_file = "uploads/resized/resized_$fileName";

$sql= mysql_query("INSERT INTO YourTableName VALUES ('','$target_file','$resized_file')") or die(mysql_error());

当然,您需要在数据库中包含一些连接。

顺便说一句,你应该重命名上传的图像。如果两个用户上传同名的图片怎么办?它将删除具有相同名称的上一个文件。