我只是不清楚如何在过去,我有让用户通过表单上传他们从自己的电脑中选取图片和JavaScript,像这样格式化这个 - :上传图像文件(不是通过表单,只是在一个文件夹中)通过PHP上传到MySQL?
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "../php/upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
}
});
}));
该文件发送到PHP脚本:
if(isset($_FILES["file"]["type"]))
{
$validextensions = array("jpeg", "jpg", "png");
$maxsize = 99999999;
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
) && ($_FILES["file"]["size"] < $maxsize)//Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
$size = getimagesize($_FILES['file']['tmp_name']);
$type = $size['mime'];
$imgfp = fopen($_FILES['file']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['file']['name'];
$sql = new mysqli("localhost","username","password","sqlserver");
$imgfp64 = base64_encode(stream_get_contents($imgfp));
$update = "UPDATE sqlserver.imageblob set image='".$imgfp64."', image_type='".$type."', image_name='".$name."', image_size='".$size."' where user_id=".$account['id'];
$sql->query($update);
然后,我已经能够显示这样和呼应HTML图像:
$imgdata = $array['image']; //store img src
$src = 'data:image/jpeg;base64,'.$imgdata;
但现在我需要上传一个图片文件次在我已经存储在一个文件夹中,即../images/image1.png
不是从窗体上传的文件。
理想我会写:
$imgfile = "../images/image1.png"
然后插入该到我代替$_FILES['file']['name']
PHP的,但我不知道如何正确地写出了这一点。我是新来的MySQL,并获取错误消息只是传递一个像上面的文件名。
如何将我已存在于我的文件夹中的图像上载到mysql表中?
我曾尝试:
也许只是创建一个HTML页面,使用PHP构建,加载所有的图像进去。每个图像都可以有一个onclick处理程序,它会为它的路径指定一些javascript变量。然后,您可以使用该路径发送ajax请求。希望这可以帮助。 – divinemaniac
尝试'on'('click',(function(e){}));' –
@divinemaniac no我不需要回显图像,只需要将它存储在mysql表中 – skyguy