I have a form and the person who fill it has the option to upload an image. The information is then stored in a table, with the person's name, e-mail, etc. and later shown in a page for the admin. I'm able to pull all the information, except I don't know how to store the image into the database and show it for the admin later. Hope you guys could understand
1条回答 默认 最新
douan8473 2014-12-23 06:17关注You don't store the images in the database, you store there path as a string
There is global variable called
$_FILESjust as$_GETand$_POSTthat contains all the info on the files uploaded. These are first stored in a temp directory. You usemove_uploaded_filesfunction to move them to a proper place. Then save the string in database.<?php $uploads_dir = '/uploads'; // Directory where you want to upload/keep all your pictures // Lets assume you sent the picture in `pictures` name field of form foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); // Save `$upload_dir/$name` in db } } ?>Now when you want to load the image later, just query the column of the table that stores the images location and use it as
srcat frontend.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报