[英]mysql - php - remove first part of url upon INSERT
我正在使用Mysql数据库和PHP执行图片库CMS。 我是新手。
我有路径问题。
这是我的文件结构:此php文档root/php/upload_portrait.php
。 我的图像存储在这里root/images/portrait_gallery/
因此,我添加了../
以将图像保存在root/images/portrait_gallery/
,效果很好。
但是在db中,URL与../存储在一起,并且该路径不正确,因为从根索引文件中调用了它们。 因此没有图像出现。
如何在INSERT INTO
数据库时删除../
?
我已经尝试了替换和更新,但无法弄清楚如何。
这是我的代码
$portrait_url= $_FILES['upload'];
// 2. connect to database:
include 'connect.php';
// 4. handle moving image from temp location to images folder (using the function billedupload)
$billedurl = billedupload($portrait_url);
if($billedurl == false){
die("Something is wrong");
}
// 5. Insert imageupload in database:
$query = "INSERT INTO portrait (portrait_id, portrait_url) VALUES ('$portrait_id', '$billedurl')";
$result = mysqli_query($dblink, $query) or die( "Forespørgsel 2 kunne ikke udføres: " . mysqli_error($dblink) );
// 6. close connection
mysqli_close($dblink);
function billedupload($filearray){
if($filearray['type']=='image/jpeg' or $filearray['type']=='image/png'){
$tmp_navn = $filearray['tmp_name'];
$filnavn = $filearray['name'];
$url = '../images/portrait_gallery/' . time() . $filnavn;
move_uploaded_file($tmp_navn, $url);
return $url;
}
else{
return false;
}
}
我相信您有2个选择。
如前所述,str_replace()可能是您需要的功能。
如果您只需要删除'/ ..'-那么,
str_replace
是PHP中的一个函数,可让您即时删除/更改部分字符串,因此,在您的代码中,
更换
$billedurl = billedupload($portrait_url);
与
$billedurl = str_replace('/..','',billedupload($portrait_url));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.