我正在使用一个文件上传表单字段,从那个字段我要编码文件名,不想要移动任何临时文件夹,直接我要插入数据库,同时直接显示图像获取数据库并显示网站,我想编码像数据:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAADIA
$path = basename($_FILES['file']['name']);//here i am getting filename
$im = file_get_contents($path);//here i am getting false
$imdata = base64_encode($im); // so can't here encode the filename
$horoscope = array("encode" => $imdata,"path" =>$path,"getcontents" =>$im);
echo json_encode($horoscope);

答案 0 :(得分:0)
PHP将文件上传到一个临时文件中并告诉你$_FILES['file']['tmp_name']
中的$_FILES['file']['name']
move_uploaded_file()
保存用户在其系统上调用文件的文件名,即他们在浏览器。
因此,如果您想在文件上$_FILES['file']['tmp']
之前获取文件,请使用$path = $_FILES['file']['tmp_name'];
$im = file_get_contents($path);
$imdata = base64_encode($im);
$horoscope = array("encode" => $imdata,
"path" =>$path,
"getcontents" =>$im
);
echo json_encode($horoscope);
$path
我不确定你传回的$_FILES['file']['name']
是什么,所以可能仍然需要$horoscope = array("encode" => $imdata,
"path" => $_FILES['file']['name'],
"getcontents" => $im
);
。
$imdata
RE:评论1
我假设您要将文件的base64encoded版本存储到数据库中,以便$path = $_FILES['file']['tmp_name'];
$file_parts = pathinfo($path);
$extn = $path_parts['extension'];
RE:评论2
要获取传入文件的扩展名,请使用pathinfo()
.png
但要注意,只是因为扩展说明.png
并不能保证它是h = """<div id="ANALYSIS" class="tabContent tabSelected">A weak handicap that looked wide open.<br><br> <b class="black">LADY MAKFI</b> showed vastly improved form to shed her maiden tag on this seasonal debut for a new yard. The filly offered little for Tony Martin last year, but did show some ability on her debut and is evidently capable when fresh. She saw it out well and it´ll be interesting to see how she copes with a rise.<br><br> <b class="black">Weardiditallgorong</b> went down fighting over this longer trip and probably improved again on her last-time-out second at Bath. This was her best effort yet on the AW.<br><br> <b class="black">Chauvelin</b>, in second-time blinkers, turned in his most encouraging effort for some time and is certainly well treated on his best form.<br><br> <b class="black">Happy Jack</b> not for the first time travelled easily until making heavy weather of it when asked for his effort. [David Orton]<br><br> <div id="resultRaceReport" class="hide"></div></div>"""
from lxml import html
x = html.fromstring(h)
div = x.xpath("//*[@id='ANALYSIS']")[0]
# find bold tags by class name
for b in div.xpath(".//b[@class='black']"):
# get bold text
print(b.text)
# get text between current bold up to next br tag.
print(b.xpath("./following::text()[1]"))
文件