[英]Upload image to MySQL from remote server PHP
I have an upload form.我有一个上传表格。 After the upload form, I have this code in PHP:
在上传表单之后,我在 PHP 中有这个代码:
$filename = $_FILES["photo"]["tmp_name"];
$destination = "hr/upload/" . $_FILES["photo"]["name"];
move_uploaded_file($filename, $destination);
$_SESSION['FORM_image'] = $destination;
This code uploads the photo on the destination directory and inserts the path in a session variable.此代码将照片上传到目标目录并在会话变量中插入路径。
Now, I want to upload this photo (the uploaded photo) in a MySQL database but my attempts fail.现在,我想将这张照片(上传的照片)上传到 MySQL 数据库中,但我的尝试失败了。
I tried to use functions like fopen() and similar, but it still not working.我尝试使用 fopen() 之类的函数,但它仍然无法正常工作。
UPDATE: Sorry, perhaps I have not explained well: the upload form and the upload on MySQL are two separated actions.更新:抱歉,可能我解释得不好:上传表单和 MySQL 上的上传是两个独立的操作。 The form uploads the photo on the remote directory.
该表单将照片上传到远程目录。 Then I want to "move" this photo from the remote directory to MySQL database.
然后我想将这张照片从远程目录“移动”到 MySQL 数据库。
Can anyone helps me?任何人都可以帮助我吗?
$myFile = "your file link goes here".
//You open file
$fileResource = fopen($myFile, "r");
//You read it and save data into $fileData variable
$fileData = fread($fileResource, filesize($myFile));
$fileData = stripslashes($fileData);
fclose($fileResource);
After that you insert that into database.之后,您将其插入到数据库中。 Consider that $fileData should be inserted into column that has format -> Blob.
考虑应该将 $fileData 插入到具有格式 -> Blob 的列中。
If you want to store the actual image into the DB , you can use the datatype BLOB for the image field.如果要将实际图像存储到 DB 中,可以对图像字段使用数据类型 BLOB。
If only path , you can use varchar如果只有 path ,则可以使用 varchar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.