繁体   English   中英

插入时删除URL的第一部分

[英]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个选择。

  1. 更改已计费的upload()的$ url行,然后删除../,因为您知道在阅读该文件时将不需要它。
  2. 更改从数据库读取的函数,然后在其中删除../。

如前所述,str_replace()可能是您需要的功能。

如果您只需要删除'/ ..'-那么,

str_replace是PHP中的一个函数,可让您即时删除/更改部分字符串,因此,在您的代码中,

更换

$billedurl = billedupload($portrait_url);

$billedurl = str_replace('/..','',billedupload($portrait_url));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM