dongshengyin0147 2014-08-23 14:04
浏览 53
已采纳

将图像路径放入mysql数据库

Edited my post so it would be more clear. I have successfully got script to upload images and their thumbnails to folder specified on webserver. I would like to put both image path and resized_image path into mysql database. I have no idea where to start. is some script necessary in upload_image_script.php or my_file.php?Please help , maybe someone can show some kind of example...

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";

 ?>

and 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条回答 默认 最新

  • dongshiru5913 2014-08-24 12:27
    关注

    You should do that in the upload_image_script.php

    First you need to create a database where you will store your images and than add to your code something like this:

    $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());
    

    Offcourse you need to include some connection to your database.

    By the way you should rename the uploaded images. What if two users upload image with same name? It would delete the previous file with the same name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记